我是iPhone新手,
我想在我的应用程序中实现单选按钮,我的应用程序中有三个按钮,按钮应该像单选按钮一样。
这是我的代码段,
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[Btn1 setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
[Btn1 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[Btn2 setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
[Btn2 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[Btn3 setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
[Btn3 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
- (IBAction)RadioButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
我们将不胜感激。
答案 0 :(得分:3)
您可以使用以下代码。
//Add all the buttons to class level NSMutable array
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[Btn1 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
[self.buttonsArray addObject:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[Btn2 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
[self.buttonsArray addObject:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[Btn3 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
[self.buttonsArray addObject:Btn3];
- (IBAction)RadioButton:(id)sender{
[self resetAllButtons];
UIButton* button=(UIButton*) sender;
[button setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateNormal];
}
-(void) resetAllButtons{
for(int i=0;i<[self.buttonsArray count];i++){
UIButton* button=[self.buttonsArray objectAtIndex:i];
[button setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
}
}
答案 1 :(得分:2)
您正在检查self.view.subviews
中的所有观看次数,但这些按钮实际上已添加到名为scrollVw的UIScrollView
(我猜)中。
将for循环更改为
for (UIButton *Radiobutton in [self.scrollVw subviews]) {
如果将scrollVw添加为 .h 文件中的属性。
答案 2 :(得分:1)
是否反对使用UISegmentedControl?它的工作方式类似于单选按钮,因为它一次只能选择一个选项。如果您对单选按钮的外观不熟,我会将其子类化以更改外观。
答案 3 :(得分:1)
或者你可以简单地使用iOS提供的控件:
UISegmentedControl
它可以轻松处理三个段,非常灵活,可以像单选按钮一样,只能选择一个段或多个段。