为2个国家设置按钮标题的更好方法

时间:2014-03-20 10:12:19

标签: objective-c uibutton xcode5

这就是我现在正在做的事情

[self.distanceFilter setTitle:strValueForDistanceFilter forState:UIControlStateNormal];
[self.distanceFilter setTitle:strValueForDistanceFilter forState:UIControlStateSelected];

这样做会有什么更好的方法,以便更改僵尸UIControlStateNormal的标题,即使选择了按钮

我尝试过的事情:

[self.distanceFilter setTitle:strValueForDistanceFilter forState:UIControlStateNormal | UIControlStateSelected];

事情是UIControlStateNormal是0.所以UIControlStateNormal | UIControlStateSelected只是UIControlStateSelected。这意味着当未选择按钮时标题不会改变。

3 个答案:

答案 0 :(得分:3)

您可以使用|执行按位OR,如下所示:

[self.distanceFilter setTitle:strValueForDistanceFilter forState:UIControlStateNormal | UIControlStateSelected];

答案 1 :(得分:2)

嗯,对于UIButton上的某个类别来说,这听起来很完美:

- (void)setTitleForAllStates:(NSString *)title
{
    for (UIControlState i = 0; i <= 3; i++)
        [self setTitle:title forState:i];
}

答案 2 :(得分:1)

其他方式,

#define UIControlStateAll UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted

[self.distanceFilter setTitle:strValueForDistanceFilter forState:UIControlStateAll];