How to remove the button tag duplicates in iPhone? - iphone

I have created buttons are programmatically and set the button tags.
View Did Load:
I have created one view and added the buttons are sub view into that view.
-(void) viewDidLoad
{
self.answerSubview = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 230)];
self.answerSubview.backgroundColor = [UIColor clearColor];
[self.view addSubview:answerSubview];
[self buttonCreation];
}
-(void) buttonCreation{
int x =100;
for(i = 0; i < 4; i++) {
UIButton *answerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[answerBtn setFrame:CGRectMake(40, x, 260, 40)];
answerBtn.tag = ar[i];
[answerBtn setTitle:[answerList objectAtIndex:i] forState:UIControlStateNormal];
[self.answerSubview addSubview:answerBtn];
x = x+50;
}
}
And i want to set the background image for the certain index of the button, when the time is finished,
-(void) timerFinished
{
for (UIView* view in [self.answerSubview subviews]) {
if([view isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton*)view;
NSLog(#"button value %d", btn.tag);
if(btn.tag == correctIndex)
{
[btn setBackgroundImage:[UIImage imageNamed:#"selected_correct_answer.png"] forState:UIControlStateNormal];
}
}
}
}
But every time,the tag values are added into the stack,I have randomly generated numbers and set it into the button tags, so now it comes like,
REsult:
First time, Second time,
0 0
1 1
3 3
2 2
3
1
2
0
(Actually my expected result is 3 1 2 0).
So how could i remove the previous tag values and get the correct tag values at a time.
Please help me out.

There are two things you can consider doing. Since the only thing that changes on every iteration is the text on the button. You could probably reuse them. Setup once and put them in ivars. On every iteration, change the text and tag of the button.
If that is not the route you're interested in, you can probably send removeFromSuperview message to those four button objects.
However, the first method is a better approach in my opinion because it will do away with continuous creation-destruction cycle.

Related

How to realize action on multiple objects

I have some values of UIButtons. Every button I have created dynamically, by this code:
-(void)AddNewTable: (NSString *) tablePic: (NSString *) addedType {
CreatedTable *ct = [[CreatedTable alloc] init];
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
NSString * uuidString = (__bridge NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
CFRelease(newUniqueId);
UIImage *tableImage = [UIImage imageNamed: tablePic];
CGRect frameBtn = CGRectMake(160.0f, 160.0f, tableImage.size.width, tableImage.size.height);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: tableImage forState:UIControlStateNormal];
[button setFrame:frameBtn];
[button addTarget:self action:#selector (draggedOut:withEvent::) forControlEvents:UIControlEventTouchDragInside];
[button setTitle: [NSString stringWithFormat:#"%d", tables.count] forState: UIControlStateNormal];
ct.Id = [uuidString lowercaseString];
ct.posX = 160;
ct.posY = 160;
ct.isActive = true;
ct.Index = button.titleLabel.text;
ct.picture = [NSString stringWithFormat:#"tables/%#", tablePic];
ct.type = addedType;
ct.angle = 0.0;
[tables addObject:ct];
[hallView addSubview:button];
}
CreatedTable - is NSObject with string params of created buttons.
As you can see, I'm adding selector for every created button. I can move every button by this selector. Here is it's code:
- (IBAction)draggedOut: (id)sender withEvent: (UIEvent *) event: (NSSet *)touches {
CreatedTable = [tables objectAtIndex: selected.[titleLabel.text intValue]]
UIButton *selected = (UIButton *)sender;
selected.center = [[[event allTouches] anyObject] locationInView:hallView];
ct.posX = selected.center.x;
ct.posY = selected.center.y; // Here I'm changing params in ct.
}
Now I need to realize multi-select (select some value of buttons by tapping on them, to make some king of a group) and after that I need to move this group (all selected buttons) like one single object.
Any suggestions how to realize it?
When the user select multiple buttons one by one , mark them all the selected buttons by changing their background image . Now when the user starts dragging any one of the selected button create all Bigger view (with the clear background color) and add the copies(new buttons look alike the selected buttons) of all the selected buttons and add them on to the bigger view . Now changing position of original button (button getting dragged) , change the position of bigger view . It will give a look and feel like all the selected button are getting dragged . Also as soon as the bigger view dragging is stopped remove all the original selected buttons from the main view .
Hope it will help you.
you could set a different Tag to each buttons in AddNewTable like this:
[button setTag:];
and then in draggedOut you can find it by:
[sender tag]

How to control ScrollView, that doesn't be repreated again and again

Do you know, how to control the ScrollView, so they do not repeat. For example I have 5 pictures (like the added figure), they should be viewable from 1-5 and it should stop right after the last picture, at the same time it should be able to scroll back and stop right after the first picture. All in all it should only be able to go from 1-5 without repeating after 5th picture and before 1st.
The problem right now is, that it scroll further after 5th, so it begins from 1st picture again, and the other way around when it scroll back to 1st picture it keep going to 5th and so on.
The code that I used for this is:
"InfinitePagingView" page!.
Disabled scroll:
scrollEnabled = NO;
What I want to know is, if I am able to stop scrolling right after 5th picture, but it should only be enable to do that on the right side. So is there any codes like: "scrollEnable right = NO & scrollEnable left = NO". If not, which code can I use.
Advance thanks
so you want infinite scrolling on the right side, but not on the left? You need to customize the InfinitePagingView, that you are using. Look in the code, especially the method scrollView:didScroll and disable it for the left side.
That is not a default behaviour, so there is not a simple property to set. But don't you think this is a strange behaviour? If you scroll right and go over the 5th picture, than you are at the 1st one again and you cannot scroll to the left anymore? Although you just came from that place?
If you don't want any infinite scrolling just use a default scrollView!
I have using following Code to display 5 UIButtons in Scrollview it works for as you want.you can use it for 5 pictures Check it.
- (void)viewDidLoad
{
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 407, 320, 80)];
[self.view addSubview:scrollview];
int i;
int spacex = 5;
int spacey=5;
for ( i = 0; i <5; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn = [[UIButton alloc] initWithFrame:CGRectMake(spacex, spacey, 115, 45)];
btn.backgroundColor = [UIColor greenColor];
[btn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
spacex = spacex + 120;
[scrollview addSubview:btn];
}
scrollview.contentSize = CGSizeMake(scrollview.frame.size.height*(i+2) , scrollview.frame.size.height);
[super viewDidLoad];
}
You can changed it according to your requirments.hope it works for you.Thanks.

UIPage Control responding only after 4th page dot

Here is the code, when pages more than 5 the dots respond only after clicking on 4th page and above, while coming reverse the 2nd and 3rd dots are selectable
if(self.eventDetailPages != Nil)
[self.eventDetailPages removeFromSuperview];
self.eventDetailPages = [[UIPageControl alloc] init];
self.eventDetailPages.numberOfPages = self.evtListData.iTotalPages;
NSLog(#"The total number of pages - %d", self.evtListData.iTotalPages);
CGSize pageControlSize = [self.eventDetailPages sizeForNumberOfPages:self.eventDetailPages.numberOfPages];
NSLog(#"The total width of pages - %f", pageControlSize.width);
self.eventDetailPages.frame = CGRectMake(0, 150, pageControlSize.width,30);
self.eventDetailPages.backgroundColor = [UIColor blackColor];
self.eventDetailPages.center = CGPointMake(self.view.frame.size.width/2, 150);
self.eventDetailPages.clearsContextBeforeDrawing = TRUE;
[self.eventDetailPages addTarget:self action:#selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.eventDetailPages];
In my case, 4 dots are there, dots respond only on or after clicking on 3rd dot and above, while coming reverse the 2nd and 3rd dots are selectable
[m_pPageControl addTarget:self action:#selector(gotonextPage:) forControlEvents:UIControlEventTouchUpInside];
-(void)gotonextPage:(UIPageControl *) pageControl
{
int page = m_pPageControl.currentPage;
NSLog(#"PAGEVALUE%d",page);
[m_pScrollView setContentOffset:CGPointMake(page * m_pScrollView.frame.size.width, 0)animated:YES];
}
What should I do? If I CLICK 2nd dot, the value of current page is not changing. if I click the 3rd then the value of current page is changing......

ASCII Value of string in button title

I have three buttons with all same property other than their title. Titles are 'A', 'B' and 'C' respectively. I wrote a method where I have written all common properties of these three buttons and calling this method where I want to add those buttons on view. I have done all theses, but struct at button Title. I used for loop to add theses buttons to view.
Here is my code:
This is method with all common properties of three buttons.
-(UIButton *)setMiddlePlayButton{
UIButton * middleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[middleButton setBackgroundImage:[UIImage imageNamed:#"Middle"] forState:UIControlStateNormal];
[middleButton addTarget:self action:#selector(playButtonPressed:)
forControlEvents:UIControlEventTouchDown];
middleButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
return middleButton;
}
Calling the method in this way to add buttons on the view.
for(int i= 0; i<3; i++){
UIButton * middleButtons = [self setMiddlePlayButton];
middleButtons.tag= 2+i;
//Here I have to add button Title which is 'A' for first button with tag 2, 'B' for second button with tag 3 and 'C' for third button.
[self.view addSubView:middleButtons];
}
I am not finding a way to get the title, is anything possible with ASCII value? Or is there any other way to do this? Please help.
Try this code
for(int i= 0; i<3; i++){
UIButton * middleButtons = [self setMiddlePlayButton];
middleButtons.tag= 2+i;
[middleButtons setTitle:[NSString stringWithFormat:#"%c", 65+i] forState:UIControlStateNormal];
[self.view addSubview:middleButtons];
}

remove button form scrollview

I added one UIScrollView in my project... and I added 10 more button "fieldButton" once, then the same UIScrollView I want add other 5 button. it I try to do this first added 10 button also coming in the scrollview .. how to remove first added 10 button before adding other item. in the same scrollview...
if(a == 1){
for(int i = 0; i< 10; i++){
UIButton *fieldButton_area = [[Mos_component alloc]getComboButton:title andFrame:CGRectMake(0, y, 180, 40)];
[fieldButton_area addSubview:cid];
[fieldButton_area addTarget:self action:#selector(get_Area:) forControlEvents:UIControlEventTouchUpInside];
[state_scroll addSubview:fieldButton_area];
}
}
else{
UIButton *fieldButton_state = [[Mos_component alloc]getComboButton:title andFrame:CGRectMake(0, y, 180, 40)];
[fieldButton_state addSubview:cid];
[fieldButton_state addTarget:self action:#selector(get_Area:) forControlEvents:UIControlEventTouchUpInside];
[state_scroll addSubview:fieldButton_state];
}
If you want just to clear your scrollview (i.e. remove all its subviews) then you can do that following way:
for (UIView* subView in [state_scroll subviews])
[subView removeFromSuperView];
If you want to remove some specific views you can check its types:
for (UIView* subView in [state_scroll subviews])
if ([subView isKindOfClass:[Mos_component class]]) // remove Mos_components only
[subView removeFromSuperView];
You can also assign tag property to you views and remove them following way:
[[fieldButton_area viewWithTag:yourTag] removeFromSuperView];
Also note that you must release your buttons somewhere or you will get memory leak.
You could also try animating the button(s) off of the screen or set the alpha's to 0 then add the new UIButton's to the view. This may use more memory, but in some cases look better.
[UIView BeginAnimation];
// set time and speed here
// Perform animation here
[UIView setAnimationDidStopSelector /* here call the method for the new button's animation into the view*/];
[UIView CommitAnimations];
// Then set the button's enabled property to NO
button.enabled = NO;
I hope this is of some assistance