20

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?

Joe Völker
  • 781
  • 1
  • 5
  • 19

2 Answers2

53

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • 1
    You're right. Reading ability IS an advantage. As I said, I feel stupid. Thank you very much! – Joe Völker Apr 08 '11 at 08:21
  • 4
    Just as a side-note; this approach does not work if you are using attributed text in IB. You have to use setAttributedTitle:forState:. – josh-fuggle Jan 31 '13 at 10:12
4

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.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393