我是 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;
}
}
任何帮助将不胜感激。