我正在做一个关于多项选择题(MCQ)的申请,我必须从网络服务中获得问题和答案。我有4个选项的1个问题,在4个选项中,我有1个正确的答案。每个选项,我都以编程方式创建了一个按钮。我就这样做了:
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];
我在问题中有多少选项将决定创建多少个按钮。现在,当我想要显示单击某个按钮时,该按钮需要保持突出显示,直到我决定更改我的答案。我做了一些方法。
我使用IndexPath
,希望我可以在indexPath.row
中使用TableView
。它根本没有用。有人可以帮忙吗?
由于
答案 0 :(得分:1)
将此更改为
[button addTarget:self action:@selector(correctPressed)
forControlEvents:UIControlEventTouchDown];
[buttonArray addObject:button];
[button addTarget:self action:@selector(correctPressed:)
forControlEvents:UIControlEventTouchDown];
[buttonArray addObject:button];
现在在您的选择器中,您将获得对该按钮的引用。
例如。
UIButton * lastButtonPressed = nil;
-(void) correctPressed:(UIButton *) theButton{
[theButton setHighlighted:YES];
if(lastButtonPressed) [lastButtonPressed setHighlighted:NO];
lastButtonPressed = theButton;
}
**您可能希望将所选方法用于按钮。因此,您可以设置一个按钮。
我刚输入此代码,检查我的@selector更改并且setHighlighted实际上是正确的,但您应该明白这一点。
现在你的代码将调用选择器,它应该有一个对调用按钮的引用。
约翰。
答案 1 :(得分:0)
没关系,我解决了这个问题。
我所做的是在代码中的某处button.tag = number; number++
。通过使用标记方法,我可以跟踪arrays
单击哪个按钮。
-(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!!");
}
答案 2 :(得分:0)
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);
} - (无效)VideoButtonSelected:(的UIButton *)发件人{ 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”]; }
在上面的代码中,我在数组计数的帮助下制作了uibutton,当我选择任何按钮时,它在最后一个按钮上显示数组的相同文本值