Yakiv Kovalskyi
Yakiv Kovalskyi

Reputation: 1757

UIButton setTitle:forState: use UIConrolState() as parameter

I always set UIButton's title as follows

aButton.setTitle(title, for: .normal)

However, recently I've seen this

aButton.setTitle(title, for: UIControlState())

Is it correct way of setting title?

Upvotes: 1

Views: 88

Answers (3)

Jagdeep
Jagdeep

Reputation: 1178

Difference between both is simple:

aButton.setTitle(title, for: .normal) 

set your text only for normal state and you have options to set some different text for other Control states as selected,focused,highlighted

but using

aButton.setTitle(title, for: UIControlState())

is something different as long as it doesnt throw any error.

Upvotes: 0

matt
matt

Reputation: 535557

Also legal and identical:

aButton.setTitle(title, for: [])

The reason is that .normal is the zero option.

Extra for experts: During the run-up to Swift 3, .normal was abolished, and [] or UIControlState() were the only ways to specify the zero option. This was because of a rule that said that the zero option of an Objective-C option set was not imported into Swift by name at all. However, this blanket application of the rule (which still exists in general) caused massive confusion and complaints, and .normal was brought back by popular demand.

Upvotes: 1

André Slotta
André Slotta

Reputation: 14040

both statements have the same result. aButton.setTitle(title, for: .normal) is the "regular way" to set the button's title though...

Upvotes: 0

Related Questions