Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text change on UIButton doesn't stick

I have an UIButton in my View that says "STOP". When pressed, it should (stop the playback, of course, and) change its label to "RTN TO ZERO". This is straightforward:

stopButton.titleLabel.text = @"RTN TO ZERO";

However, the change appears only for a split second. It doesn't stick. I assume that the button (which gets highlighted when pressed) accepts and displays the new label, but somehow the highlight is reversed only later, restoring the button to the look it had before it was pressed, not honoring the label text change. The button is conceived in IB, not programmatically.

I feel stupid. Can someone please point me in the right direction?

like image 410
Joe Völker Avatar asked Apr 07 '11 10:04

Joe Völker


2 Answers

In the button handler, try this:

[stopButton setTitle:@"RTN TO ZERO" forState:UIControlStateNormal];

Instead of directly changing text property of titleLabel use setTitle:forState: method to set the title in different states. Please check the manual for the details of available states.

like image 175
taskinoor Avatar answered Nov 20 '22 01:11

taskinoor


Swift version

myButton.setTitle("button text", for: UIControl.State.normal)

Use setAttributedTitle:for for attributed text. See here for how to make attributed strings in Swift.

like image 42
Suragch Avatar answered Nov 20 '22 00:11

Suragch