carloabelli
carloabelli

Reputation: 4349

UIButton Title In .xib Overriding Programmatic Title Change

I am writing an app which loads a view (designed in a .xib) with multiple buttons. When loading the view I set the buttons titles programmatically. I then add the view as a subview. For a fleeting second I see the programatically set titles appear and then they change to the prototype titles in the .xib.

Here is the code:

self.cellPhoneButton.titleLabel.text = person.cellPhone;
self.homePhoneButton.titleLabel.text = person.homePhone;

// Do other stuff with the view

[self.peopleView addSubview:self.personView];

Why do the titles of the buttons revert back to their default content, and why is this not happening to the other components of the page (Labels, Navigation Bar Title, etc.)?

EDIT:

I also tried this code without success:

[self.cellPhoneButton setTitle:person.cellPhone forState:UIControlStateNormal|UIControlStateSelected];
[self.homePhoneButton setTitle:person.homePhone forState:UIControlStateNormal|UIControlStateSelected];

// Do other stuff with the view

[self.peopleView addSubview:self.personView];

Upvotes: 1

Views: 950

Answers (1)

chancyWu
chancyWu

Reputation: 14423

You should use [self.homePhoneButton setTitle: forState:]; method to set the UIButton's title. and the state in your case should be UIControlStateNormal which means control state will apply to all the states if you don't specify the others explicitly. If you combine the Normal with others, then Normal will be overidded.

Upvotes: 5

Related Questions