Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does not work changing UIButton text via dot syntax?

Is it possible to change UIButton title label text via dot syntax? The text is not changing if I do it via dot syntax:

self.myButton.titleLabel.text = [NSString stringWithString:@"Nop"];

But this one does:

[self.myButton setTitle: @"Yep" forState: UIControlStateNormal];

Text change works for pure UILabels, is UIButton a special case or something?

like image 510
Centurion Avatar asked May 25 '26 04:05

Centurion


2 Answers

This is because the text can be changed for a UIButton for several different states (Normal, Highlighted, etc). Apple simply chose to require explicit code to set the text. You could probably create a category to implement this, if you really wanted.

like image 52
FreeAsInBeer Avatar answered May 27 '26 16:05

FreeAsInBeer


UIButton's can reconfigure their titles for any of the UIControlState's (see here: UIControlState).

Since using the dot syntax doesn't set the title for any specific state, the UIButton is likely re-configuring the title for UIControlStateNormal, which is set to nothing.

like image 34
thomashw Avatar answered May 27 '26 17:05

thomashw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!