UIButton:当点击时改变titleColor,当otherButton被点击时返回



当我点击

时,我想让按钮的titleColor从白色变为红色

但是当我点击其他按钮时,我想让按钮变回白色

我该怎么做呢?

这是我写的代码,但它不是我想要的工作。

[aButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

使用setSelected:改变按钮的选择状态。

在viewDidLoad:

[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[bButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[bButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
aButton.selected = YES;

- (IBAction)aButton_or_bButtonClicked:(id)sender {
    if(aButton.selected) {
        aButton.selected = NO;
        bButton.selected = YES;
    }
    else {
        aButton.selected = YES;
        bButton.selected = NO;
    }
}

应该是这样:

- (IBAction)aButtonClicked:(id)sender {
    [aButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
- (IBAction)bButtonClicked:(id)sender {
    [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

最新更新