Iphone 如何定位单击的以编程方式创建的按钮?
我正在做一个关于多项选择题(MCQ)的应用程序,在这里我必须从一个Web服务获取问题和答案。我有一个问题和4个选项,在4个选项中,我有1个正确答案。在每个选项中,我都以编程方式创建了一个按钮。我就是这样做的:Iphone 如何定位单击的以编程方式创建的按钮?,iphone,iphone-sdk-3.0,Iphone,Iphone Sdk 3.0,我正在做一个关于多项选择题(MCQ)的应用程序,在这里我必须从一个Web服务获取问题和答案。我有一个问题和4个选项,在4个选项中,我有1个正确答案。在每个选项中,我都以编程方式创建了一个按钮。我就是这样做的: UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(40, yButton, 30, 30); [button setTitl
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];
一个问题中有多少选项将决定将创建多少按钮。现在,当我想显示一个指示,当某个按钮被点击时,该按钮需要保持高亮显示,直到我决定更改我的答案。我已经做了一些方法
我使用了indepath
,希望能像TableView
一样使用indepath.row
。这根本不起作用。有人能帮忙吗
谢谢将此更改为
[button addTarget:self action:@selector(correctPressed)
forControlEvents:UIControlEventTouchDown];
[buttonArray addObject:button];
[button addTarget:self action:@selector(correctPressed:)
forControlEvents:UIControlEventTouchDown];
[buttonArray addObject:button];
现在,在选择器中,您将获得对按钮的引用
例如
**您可能希望对按钮使用选定的方法。因此,您可以设置一个选中的按钮
我刚刚输入了这段代码,检查我的@selector更改,setHighlighted实际上是正确的,但你应该明白这一点
现在,您的代码将调用选择器,它应该有一个对调用按钮的引用
约翰。没关系,我解决了这个问题 我做的是做了一个
button.tag=number;数字+++
代码中的某个地方。通过使用标记方法,我可以在数组中跟踪单击的按钮
-(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!!");
}
for(int i=0;iThanks John,将试用该代码。您能解释一下为什么要使用按钮(ui button*)吗?编辑:我已经试用了该代码,但效果不是我想要的。我希望它高亮显示,直到我再次点击其他按钮或它本身。[按钮设置高亮显示:是]当我松开手指时,
将变为否
。对不起,约翰,我觉得我在打扰你。但它仍然不起作用。它对你有用吗?当你点击按钮时,当你松开手指时,它仍然高亮显示?
-(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!!");
}
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);