iPhone初心者ですが、
アプリにラジオ ボタンを実装したいのですが、アプリには 3 つのボタンがあり、ボタンはラジオ ボタンのように動作する必要があります。
これが私のコードスニペットです。
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;
}
}
どんな助けでも大歓迎です。