Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to update UIButton title/text programmatically?

I have a UIButton, that when pressed, brings up a new view where the user can change some settings. When the view is dismissed, I'd like to update the title/text of the UIButton to reflect the new state. I'm calling:

[myButton setTitle: @"myTitle" forState: UIControlStateNormal]; [myButton setTitle: @"myTitle" forState: UIControlStateApplication]; [myButton setTitle: @"myTitle" forState: UIControlStateHighlighted]; [myButton setTitle: @"myTitle" forState: UIControlStateReserved]; [myButton setTitle: @"myTitle" forState: UIControlStateSelected]; [myButton setTitle: @"myTitle" forState: UIControlStateDisabled]; 

But it never seems to change from the original text/title as specified in IB.

like image 804
George Armhold Avatar asked Jun 23 '09 16:06

George Armhold


People also ask

How do I change text on UIButton?

Programmatically. To make a multi-line text in UIButton, you insert a new line character ( \n ) wherever you want in button title and set lineBreakMode to byWordWrapping . You can adjust text alignment with . textAlignment .

How do I change the text of a button in Swift?

You can omit UIControlState part and just write like button. setTitle("my text here", forState: . Normal) .

How do I change the label text on a button click in Swift?

Open your storyboard and view controller source side by side in the assistant editor. Then select the label. Switch to the connections inspector and then drag from the small circle next to "New Referencing Outlet" and into the view controller source file. This should create the @IBOutlet for you.


1 Answers

I solved the problem just setting the title parameter for UIControlStateNormal, and it automatically works on the other states. The problem seems to be when you set another UIControlState.

[myButton setTitle: @"myTitle" forState: UIControlStateNormal]; 
like image 197
Yotes Avatar answered Sep 20 '22 04:09

Yotes