有没有办法在UIButton上setTitle一次更新所有UIControlStates?

时间:2021-10-24 09:35:07

I have a UIButton, and I'd like to update its title, but I'd rather not have to always do it for each and every state like the following:

我有一个UIButton,我想更新它的标题,但我不必总是为每个状态执行此操作,如下所示:

[myButton setTitle:@"Play" forState:UIControlStateNormal];

[myButton setTitle:@"Play" forState:UIControlStateHighlighted];

[myButton setTitle:@"Play" forState:UIControlStateSelected];

Is there a better way?

有没有更好的办法?

3 个解决方案

#1


58  

According to the documentation you should only need to call:

根据文档,您只需要致电:

[myButton setTitle:@"Play" forState:UIControlStateNormal];

The UIButton docs explain why:

UIButton文档解释了原因:

In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the value for UIControlStateNormal is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

通常,如果没有为状态指定属性,则默认使用UIControlStateNormal值。如果未设置UIControlStateNormal的值,则该属性默认为系统值。因此,您应该至少设置正常状态的值。

Namely, that if you only set the normal value the other states will refer to it when being set.

也就是说,如果您只设置正常值,则其他状态将在设置时引用它。

#2


6  

or you can setTitle by :

或者您可以通过以下方式设置标题:

[myButton setTitle:@"Play" forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];

#3


0  

You can create a category for UIButton:

您可以为UIButton创建一个类别:

@implementation UIButton (Addition)
-(void)setTitleForAllStates:(NSString *)title {
     //you can add/remove this area : UIControlStateApplication, UIControlStateDisabled, UIControlStateReserved
     [self setTitle:title forState:UIControlStateNormal];
     [self setTitle:title forState:UIControlStateSelected];
     [self setTitle:title forState:UIControlStateHighlighted];
}
@end

#1


58  

According to the documentation you should only need to call:

根据文档,您只需要致电:

[myButton setTitle:@"Play" forState:UIControlStateNormal];

The UIButton docs explain why:

UIButton文档解释了原因:

In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the value for UIControlStateNormal is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

通常,如果没有为状态指定属性,则默认使用UIControlStateNormal值。如果未设置UIControlStateNormal的值,则该属性默认为系统值。因此,您应该至少设置正常状态的值。

Namely, that if you only set the normal value the other states will refer to it when being set.

也就是说,如果您只设置正常值,则其他状态将在设置时引用它。

#2


6  

or you can setTitle by :

或者您可以通过以下方式设置标题:

[myButton setTitle:@"Play" forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];

#3


0  

You can create a category for UIButton:

您可以为UIButton创建一个类别:

@implementation UIButton (Addition)
-(void)setTitleForAllStates:(NSString *)title {
     //you can add/remove this area : UIControlStateApplication, UIControlStateDisabled, UIControlStateReserved
     [self setTitle:title forState:UIControlStateNormal];
     [self setTitle:title forState:UIControlStateSelected];
     [self setTitle:title forState:UIControlStateHighlighted];
}
@end