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?
A control that executes your custom code in response to user interactions.
You can omit UIControlState part and just write like button. setTitle("my text here", forState: . Normal) . I think it helps to mention: button is an IBOutlet (in contrast with the IBAction which contains this line of code) if you're leveraging Storyboard.
To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen.
UIButton has a special method for setting the label.
For instance
NSString *buttonText = [NSString stringWithFormat:@"%@",stringifiedVariable];
[ViewClass.ButtonOutlet setTitle:buttonText forState:UIControlStateNormal];
Check the documentation for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With