1

我是 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;
    }
}

任何帮助将不胜感激。

4

4 回答 4

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];
           }
}
于 2012-09-06T12:30:52.980 回答
2

您正在检查所有视图,self.view.subviews但按钮实际上已添加到UIScrollView(我猜)称为 scrollVw 的。

将 for 循环更改为

    for (UIButton *Radiobutton in [self.scrollVw subviews]) {

如果 scrollVw 作为属性添加到.h文件中。

于 2012-09-06T11:39:08.030 回答
1

反对使用 UISegmentedControl 吗?它就像一个单选按钮,因为它一次只能选择一个选项。如果您对单选按钮的外观不感兴趣,我会将其子类化以更改外观。

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html

于 2012-09-06T11:39:25.590 回答
1

或者,您可以简单地使用 iOS 提供的 Control 来完成类似的操作:

UISegmentedControl

它可以轻松处理三个段,非常灵活,并且可以像一个单选按钮一样,您只能选择一个段或多个段。

于 2012-09-06T11:43:04.343 回答