views:

31

answers:

2

In IB I have a view with a UIButton of Type Custom, it has no image and the Title is set as "#placeholder"

The view is attached to a class that provides an IBOutlet and an IBAction for the button.

I set the button title with this: ViewClass.ButtonOutlet.titleLabel.text = @"%@",stringifiedVariable; whenever i need to.

This works ok. However, when i click the button, the titleLabel.text reverts back to "#placeholder".

I tried unchecking 'Highlighted Adjust Image' in the Drawing section (attributes tab) in the Inspector but the behaviour was still the same.

Can this change be prevented? or is there a better pattern that i should be using?

+1  A: 

UIButton has a special method for setting the label.

  • (void)setTitle:(NSString *)title forState:(UIControlState)state

For instance

NSString *buttonText = [NSString stringWithFormat:@"%@",stringifiedVariable];
[ViewClass.ButtonOutlet setTitle:buttonText forState:UIControlStateNormal];

Check the documentation for more details.

Jongsma
Thanks for the quick answer
Luke Mcneice
A: 

If you want the text to not change when you click on it, the simplest way is take the title out of the button in IB and then use the setTitle: forState: method from Jongsma.

Jesse Naugher
I originally tried this; the button did not display any text at all
Luke Mcneice