is it possible to update UIButton title/text programmatically?

IosIphoneUibuttonUicontrolstate

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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];

Solution 2 - Ios

Do you have the button specified as an IBOutlet in your view controller class, and is it connected properly as an outlet in Interface Builder (ctrl drag from new referencing outlet to file owner and select your UIButton object)? That's usually the problem I have when I see these symptoms.


Edit: While it's not the case here, something like this can also happen if you set an attributed title to the button, then you try to change the title and not the attributed title.

Solution 3 - Ios

As of Swift 4:

    button.setTitle("Click", for: .normal)

Solution 4 - Ios

I discovered another problem. It may be a bug introduced in iOS 5, but I thought I'd point it out for anyone else who encounters it.

If you don't set any default text for the button in the XIB, no text will ever appear if you set it programmatically. And if you do set text in the XIB, any text you subsequently assign to the button programmatically will be truncated to the size of the default text.

And finally, if you're showing the view with your button and then invoke another view (like an ActionSheet) and then dismiss it, the text that you assigned to the button programmatically will be erased and the button caption will return to whatever you set up in the XIB.

Solution 5 - Ios

Even though Caffeine Coma's issue was resolved, I would like to offer another potential cause for the title not showing up on a UIButton.

If you set an image for the UIButton using

- (void)setImage:(UIImage *)image forState:(UIControlState)state

It can cover the title. I found this out the hard way and imagine some of you end up reading this page for the same reason.

Use this method instead

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

for the button image and the title will not be affected.

I tested this with programmatically created buttons and buttons created in a .xib

Solution 6 - Ios

for swift :

button.setTitle("Swift", forState: UIControlState.Normal)                   
 

               

Solution 7 - Ios

Turns out the docs tell you the answer! The UIButton will ignore the title change if it already has an Attributed String to use (with seems to be the default you get when using Xcode interface builder).

I used the following:

[self.loginButton 
     setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Error !!!" attributes:nil] 
     forState:UIControlStateDisabled];
[self.loginButton setEnabled:NO];

Solution 8 - Ios

One more possible cause is this:

If you attempt to set the button's title in the (id)initWithNibName: ... method, then you're button property will still be nil. It hasn't yet been assigned to the UIButton.

You must be sure that you're setting your buttons in a method like (void)viewWillLoad or (void)viewWillAppear, but you probably don't want to set them as late as (void)viewDidAppear.

Solution 9 - Ios

Sometimes it can get really complicated. The easy way is to "refresh" the button view!

//Do stuff to your button here.  For example:

[mybutton setEnabled:YES];

//Refresh it to new state.

[mybutton setNeedsDisplay];

Solution 10 - Ios

I kept having problems with this, the only solution was to add an image and label as subviews to the uibutton. Then I discovered that the main problem was that I was using a UIButton with title: Attributed. When I changed it to Plain, just setting the titleLabel.text did the trick!

Solution 11 - Ios

@funroll is absolutely right. Here you can see what you will need https://stackoverflow.com/questions/7852737/make-sure-function-runs-on-main-thread-only. If you do not want deal with threads you can do like this for example: create NSUserDefaults and in ViewDidLoad cheking condition was pressed button in another View or not (in another View set in NSUserDefaults needed information) and depending on the conditions set needed title for your UIButton, so [yourButton setTitle: @"Title" forState: UIControlStateNormal];

Solution 12 - Ios

Make sure you're on the main thread.

If not, it will still save the button text. It will be there when you inspect the object in the debugger. But it won't actually update the view.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeorge ArmholdView Question on Stackoverflow
Solution 1 - IosYotesView Answer on Stackoverflow
Solution 2 - IosKen PespisaView Answer on Stackoverflow
Solution 3 - IosAllan SpreysView Answer on Stackoverflow
Solution 4 - IosOscarView Answer on Stackoverflow
Solution 5 - IosJesse BlackView Answer on Stackoverflow
Solution 6 - IosDara TithView Answer on Stackoverflow
Solution 7 - IosCPDView Answer on Stackoverflow
Solution 8 - IosRamseyView Answer on Stackoverflow
Solution 9 - IosMike RapadasView Answer on Stackoverflow
Solution 10 - IosMarkView Answer on Stackoverflow
Solution 11 - IosdaleijnView Answer on Stackoverflow
Solution 12 - IosfunrollView Answer on Stackoverflow