Mina Wissa
Mina Wissa

Reputation: 10971

iOS SDK: Difference between UIButton setTitleForState and UIButton titleLabel.text

I have this issue with a custom UIView where I have a UIButton subview, I want to set the button's text on initialization based on some condition like this:

- (void)awakeFromNib {
    [super awakeFromNib];
//check for some conditions
self.testButton.titleLabel.text=@"Some Title";
}

Nothing happens and the button's text is the same as defined in the nib file, however if I change the implementation to:

- (void)awakeFromNib {
    [super awakeFromNib];
//check for some conditions
    [self.testButton setTitle:@"Some Title" forState:UIControlStateNormal];
}

It works as expected.

Can somebody please explain to me the difference between the two approaches? and when to use each?

EDIT:

the suggested answer doesn't explain my situation, I tested changing the button's text from another button's action like this:

- (IBAction)otherButtonClicked:(id)sender {
self.testButton.titleLabel.text=@"Some Title";
}

and the button's text changed. I just want to understand that behaviour.

Upvotes: 1

Views: 146

Answers (3)

Gero
Gero

Reputation: 4434

The accepted answer is correct, I just add this here to elaborate a bit (comments are too short)

There's not much to explain here. The title is simply not meant to set the text at all. My guess is that the internal workings of UIButton make it save the text somewhere else as well (for different states). It uses a normal UILabel to eventually display that, because that's convenient and easy to understand. Setting the text of that does not change the button in all cases, which probably ultimately depends on the drawing cycle. I assume when it's drawn, laid out, or the like it "replaces" the label's text with its other saved variant at some point.

Now you might wonder why Apple did then expose the UILabel and thus seemed to make the text editable then.

Legacy is probably one aspect of this decision (IIRC you could once set the button's title that way). Although old code doesn't result in the desired behavior, it at least didn't crash immediately. Also, a lot of code simply wants to get the text and expects a label, that works perfectly fine as ever.

Second, designing it totally different seems overkill. To avoid that, they would have to use a subclass of UILabel which prevents you from setting the text or something and use that. Or skip it (and thus legacy support) completely and only offer the setTitle:forState: method. That seems like a bit much for a simple text container like a Label.

Ultimately it's a design choice made by Apple. You can't set the title text directly and there's no case in which you should do it any way other than by using setTitle:forState:

Upvotes: 2

user3182143
user3182143

Reputation: 9599

Exact answer is

titleLabel

Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.

The titleLabel property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.

setTitle

Use this method to set the title for the button. The title you specify derives its formatting from the button’s associated label object. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.

At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.

Upvotes: 2

Shubhank
Shubhank

Reputation: 21805

Title label access is given to adjust other properties of the label such as font but setting titleLabel text does not work.

It is because UIButton class has inner implementation to set the text based on different states of the button like selected/highlighted etc which overrides label text.

Upvotes: 2

Related Questions