UIButton:单击时更改titleColor,单击otherButton时更改回来

时间:2014-06-29 12:49:34

标签: ios uibutton

当我点击

时,我希望aButton将titleColor从WHITE更改为RED

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

我该怎么做?

这是我写的代码,但它不能按我的意愿工作。

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

2 个答案:

答案 0 :(得分:1)

使用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;
    }
}

答案 1 :(得分:0)

应该像这样工作:

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

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