Setting titleLabel for UIButton

I was a little embarrassed to find today that when I try to change the text shown on UIButton, the following does not work:

// 001
[[[self button] titleLabel] setText:@"Peanuts"];

I think I know why, but I just wanted to confirm my opinion here. 001 sets the titleLabel text property, but like its a UILabel( subview UIButton), it does not cause the user interface to be redrawn. This leads to an internal change in the text property UILabel, but, unfortunately, there is no visual change in the user interface.

// 002
[[self button]setTitle:@"Peanuts" forState:UIControlStateNormal];

It would seem that the setTitle: forState method is the right way, it has additional overhead that requires state, but causes the interface to be redrawn because it is called directly on UIButton. My question is, is 002 the right way to do this, it would seem, if I am not doing something completely wrong?

+5
source share
1 answer

You're right.

[[self button]setTitle:@"Peanuts" forState:UIControlStateNormal];

- This is the way button names should be given. This allows you to control how your button looks in all its various control states.

From the docs:

, , UIControlStateNormal. UIControlStateNormal , . , , .

+7

All Articles