Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to setTitle on a UIButton that updates all UIControlStates at once?

Tags:

swift

iphone

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:

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

Is there a better way?

like image 404
Chris Craft Avatar asked Sep 03 '09 18:09

Chris Craft


2 Answers

According to the documentation you should only need to call:

In Objective-C:

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

In Swift:

myButton.setTitle("Play", for: .normal) 

The UIButton docs explain why:

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.

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

like image 88
fbrereto Avatar answered Nov 04 '22 03:11

fbrereto


or you can setTitle by :

[myButton setTitle:@"Play" forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected]; 
like image 26
ihsan_husnul Avatar answered Nov 04 '22 03:11

ihsan_husnul