Joe Völker
Joe Völker

Reputation: 791

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?

Upvotes: 20

Views: 10790

Answers (2)

Suragch
Suragch

Reputation: 511646

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.

Upvotes: 4

taskinoor
taskinoor

Reputation: 46027

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.

Upvotes: 53

Related Questions