How to locate which programmatically created button is clicked?
I am doing an application on a Multiple Choice Question (MCQ) where I have to get questions, answers from a webservice. I have 1 ques开发者_如何学Ction with 4 options, among the 4 options, I have 1 correct answer. Each of the options, I created a button programmatically. This is how I did it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(40, yButton, 30, 30);
[button setTitle:@" " forState:UIControlStateNormal];
[button addTarget:self action:@selector(correctPressed)
forControlEvents:UIControlEventTouchDown];
[buttonArray addObject:button];
How many options I have in a questions will determine how many buttons will be created. Now when I want to show an indication that when a certain button is clicked, that button needs to stay highlighted until I decides to change my answer. I have done a few approaches.
I used IndexPath
, hoping I could use indexPath.row
like in TableView
. It didnt work out at all. Can anyone help?
Thanks
Change this to
[button addTarget:self action:@selector(correctPressed)
forControlEvents:UIControlEventTouchDown];
[buttonArray addObject:button];
[button addTarget:self action:@selector(correctPressed:)
forControlEvents:UIControlEventTouchDown];
[buttonArray addObject:button];
now in your selector you will get a reference to the button.
eg.
UIButton * lastButtonPressed = nil;
-(void) correctPressed:(UIButton *) theButton{
[theButton setHighlighted:YES];
if(lastButtonPressed) [lastButtonPressed setHighlighted:NO];
lastButtonPressed = theButton;
}
** You may want to use the selected Method for buttons. So you can set a button selected instead.
I just typed this code, check my @selector change and that setHighlighted is actually correct but you should get the idea.
Now your code will call the selector and it should have a reference to the calling button.
John.
for(int i=0; i<[videolistArray count];i++){
NSArray *myWords = [[[videolistArray objectAtIndex:i] valueForKey:@"video"] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
//NSLog(@"%@",[myWords lastObject]);
UIButton *guitaristButton = [[UIButton alloc]init];
guitaristButton.frame = CGRectMake(0, ycomp, 300, 35);
guitaristButton.tag = i;
[guitaristButton setBackgroundImage:[UIImage imageNamed:@"green-buttons.png"] forState:UIControlStateNormal];
[guitaristButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[guitaristButton setTitle:[myWords lastObject] forState:UIControlStateNormal];
[guitaristButton addTarget:self action:@selector(VideoButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
//[guitaristButton performSelector:<#(SEL)aSelector#> withObject:<#(id)object1#> withObject:<#(id)object2#>
[videoScrollView addSubview:guitaristButton];
[guitaristButton release];
ycomp = ycomp+35;
}
videoScrollView.contentSize=CGSizeMake(300, ycomp);
} -(void)VideoButtonSelected:(UIButton*)sender{ NSArray *myWords = [[[videolistArray objectAtIndex:sender.tag] valueForKey:@"video"] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]]; //NSLog(@"%@",[myWords lastObject]); [setAlarmVedioButton setTitle:[myWords lastObject] forState:UIControlStateNormal]; [videoNameButtonView removeFromSuperview]; vedioString=[[videolistArray objectAtIndex:sender.tag] valueForKey:@"video"] ; }
in above code me make uibutton with help of array count and when i select any button then it display the same text value of array on last button
Its alright, I solved this issue.
What I did was did a button.tag = number; number++
somewhere in the codes. By using tagging methods, I then can keep track in the arrays
which button is clicked.
-(void)correctAnswer
{
yButton += 50;
yLabel += 50;
label = [[UILabel alloc] initWithFrame:CGRectMake(75, yLabel, 150, 30)];
label.backgroundColor = [UIColor clearColor];
[label setText:Option];
[labelArray addObject:label];
[self addSubview:label];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(40, yButton, 30, 30);
button.tag = idx;
NSLog(@"-------====== Tag Index is: %i", idx);
UIImage * btnImage1 = [UIImage imageNamed:@"uncheckedCheckbox.png"];
[button setImage:btnImage1 forState:UIControlStateNormal];
[button addTarget:self action:@selector(correctPressed:)
forControlEvents:UIControlEventTouchUpInside];
[buttonArray addObject:button];
idx++;
[self addSubview:button];
}
-(void)correctPressed:(id)sender {
int but = (int)[(UIButton*)sender tag];
if(correctValue) {
UIImage * btnImage1 = [UIImage imageNamed:@"uncheckedCheckbox.png"];
UIButton *btn1 = [buttonArray objectAtIndex:but];
[btn1 setImage:btnImage1 forState:UIControlStateNormal];
[btn1 setImage:btnImage1 forState:UIControlStateNormal];
[btn1 setTitle:@"" forState:UIControlStateNormal];
correctValue = NO;
} else {
UIImage * btnImage2 = [UIImage imageNamed:@"checkedCheckbox.png"];
UIButton *btn2 = [buttonArray objectAtIndex:but];
[btn2 setImage:btnImage2 forState:UIControlStateNormal];
[btn2 setImage:btnImage2 forState:UIControlStateNormal];
[btn2 setTitle:@"" forState:UIControlStateNormal];
correctValue = YES;
}
NSLog(@"Correct answer!!");
}
精彩评论