1

I know this question must be answered before but still I'd like to know why first line of code couldn't change title of the UIButton while the second can. Besides first line is copied from a Objective-C book of BigNerdRanch...which I take as sample that works.

self.assetTypeButton.titleLabel.text=@"foo"; //doesn't work

[self.assetTypeButton setTitle:@"foo" forState:UIControlStateNormal]; //this works and button title is changed.
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Alan Hoo
  • 445
  • 3
  • 12
  • [read this](http://stackoverflow.com/a/11417134/1066828) & [this](http://stackoverflow.com/a/4910521/1066828) – Fahim Parkar Jul 24 '16 at 15:20
  • 1
    Possible duplicate of [iOS: UIButton titleLabel -- does it do anything at all?](http://stackoverflow.com/questions/4910446/ios-uibutton-titlelabel-does-it-do-anything-at-all) – Lorenzo B Jul 24 '16 at 15:22

1 Answers1

0

The better explanation from Apple TitleLabel and setTitle

titleLabel

UIButton *button  = [UIButton buttonWithType: UIButtonTypeSystem];
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode   = NSLineBreakByTruncatingTail;

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.

user3182143
  • 9,459
  • 3
  • 32
  • 39