以前的单选按钮状态未清除目标c

时间:2019-06-12 21:07:31

标签: objective-c uiscrollview uibutton radio-button

我正在创建一个自定义单选按钮,我应该只能选择一个单选按钮,如果单击下一个单选按钮,则应该可以取消选择先前选择的单选按钮。

这是我的代码:

 - (void)setupBtn{
        self.radioBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        [self.radioBtn setFrame:CGRectMake(40,38,18,18)];
        [self.radioBtn setImage: [UIImage imageNamed:@"radio-button"]forState:UIControlStateNormal];
        [self.radioBtn setImage: [UIImage imageNamed:@"radio-button-select"]forState: UIControlStateSelected];
        [self.radioBtn addTarget:self action:@selector(radioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self.view2 addSubview:self.radioBtn];

    }
    - (IBAction)radioButtonTapped:(UIButton*)sender{
        if (self.radioBtn.isSelected ==  true) {
            [self.radioBtn setSelected:NO];
            self.view1.backgroundColor =[UIColor clearColor];
            [self.radioBtn setImage:[UIImage imageNamed:@"radio-button"] forState:UIControlStateNormal];
        }else {
            [self.radioBtn setSelected:YES];
            self.view1.backgroundColor = [UACFCustomColors continentalLightBlueTransparent];
            [self.radioBtn setImage:[UIImage imageNamed:@"radio-button-select"] forState:UIControlStateSelected];
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试将所有按钮存储在数组中。当一个被点击时,遍历数组,并检查是否选择了每个按钮。如果选择了按钮,则将其设置为不,然后退出循环。循环后,将被点击的按钮(sender)设置为选定。

未经测试的示例代码:

- (void) viewDidLoad {
    self.radioBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    [self.radioBtn setFrame:CGRectMake(40,38,20,20)];
    [self.radioBtn setImage: [UIImage imageNamed:@"radio-button"]forState:UIControlStateNormal];
    [self.radioBtn setImage: [UIImage imageNamed:@"radio-button-select"]forState: UIControlStateSelected];
    [self.radioBtn addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.radioBtn setSelected:YES];
    [self.view2 addSubview:self.radioBtn];

    self.radioBtn2 = [UIButton buttonWithType: UIButtonTypeCustom];
    [self.radioBtn2 setFrame:CGRectMake(40,38,20,20)];
    [self.radioBtn2 setImage: [UIImage imageNamed:@"radio-button"]forState:UIControlStateNormal];
    [self.radioBtn2 setImage: [UIImage imageNamed:@"radio-button-select"]forState: UIControlStateSelected];
    [self.radioBtn2 addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.radioBtn2 setSelected:NO];
    [self.view2 addSubview:self.radioBtn2];

    self.radioBtn3 = [UIButton buttonWithType: UIButtonTypeCustom];
    [self.radioBtn3 setFrame:CGRectMake(40,38,20,20)];
    [self.radioBtn3 setImage: [UIImage imageNamed:@"radio-button"]forState:UIControlStateNormal];
    [self.radioBtn3 setImage: [UIImage imageNamed:@"radio-button-select"]forState: UIControlStateSelected];
    [self.radioBtn3 addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.radioBtn3 setSelected:NO];
    [self.view2 addSubview:self.radioBtn3];
    self.buttonArray = @[self.radioBtn, self.radioBtn2, self.radioBtn3];

}

- (void) myButtonTapped:(UIButton*) sender {
    for(UIButton *btn in self.buttonArray) {
        // Only one button should be selected at a time, so once we find it,
        // set it to not be selected then break from the loop
        if(btn.isSelected){
            [btn setSelected:NO];
            break;
        }
    }
    // set the button that was tapped to be selected
    [sender setSelected:YES];
}

将一个按钮的文本发送到另一个按钮的文本非常简单。

[btn2 setTitle:[btn1 titleForState:UIControlStateNormal] forState:UIControlStateNormal];