neutrino
neutrino

Reputation: 2317

UIButton title text is not updated even if I update it in Main thread

I'm trying to change the title of an UIButton I've created programmatically, when the user clicks in it. So, this is my code to create the UIButton:

myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[myButton setBackgroundColor:[UIColor blackColor]];
[myButton setAlpha:0.7];
[myButton setTitle:@"Hello" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(userClicked:) forControlEvents:UIControlEventTouchUpInside];

[parentView addSubview:myButton];

And, in my userClicked: method, I do:

-(void) userClicked:(UIButton*)button
{
    NSLog(@"USER CLICKED!!!");
    if ([NSThread isMainThread])
    {
        NSLog(@"is main thread");
    }

    [button setTitle:@"Bye" forState:UIControlStateHighlighted];
    [button setTitle:@"Bye" forState:UIControlStateNormal];
    [button setTitle:@"Bye" forState:UIControlStateSelected];

    [self someLengthyComputation];
}

The weird thing is that I can see the log messages printed:

USER CLICKED!!! 
isMainThread

But, the title of the button does not change! What am I doing wrong?

EDIT: Setting the title for several states doesn't work either.

EDIT2: If I print the description of button in the debugger window of Xcode, it shows the right title!

Printing description of button->_titleView:
<UIButtonLabel: 0xa4c9310; frame = (95 216; 130 22); text = 'Bye'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa44f080>>

Upvotes: 46

Views: 43723

Answers (24)

zero3nna
zero3nna

Reputation: 2918

For Swift 3 to 5 just use the following:

button.setTitle("My title", for: .normal)

or for a attributed text use this:

button.setAttributedTitle(<AttributedString>, for: .normal)

Upvotes: 9

Divyanshu Kumar
Divyanshu Kumar

Reputation: 1451

instead of yourButton.titleLabel?.text = "T"

use this yourButton.setTitle("TITLE", for: .normal)

Upvotes: 0

hyouuu
hyouuu

Reputation: 2491

None of the above worked for my case (I was having a button on top of a view presented with UIPopoverController), and I had to removeFromSuperview before setTitle, then addSubview back - hope this helps someone with similar issues

Upvotes: 1

jenish
jenish

Reputation: 268

There is no requirement to use:

[button setNeedsLayout];
[button layoutIfNeeded];

Instead, first set type as DEFAULT to CUSTOM

If you applied setAttributedTitle then use:

[button setAttributedTitle:[NSAttributedString new] forState:UIControlStateNormal];

Otherwise there is no need to change anything.

If the color of the text has not changed then apply same thing and set title color with:

[button setTitleColor:[any_color] forState:UIControlStateNormal];

Upvotes: 1

Amr Angry
Amr Angry

Reputation: 3831

Conclusion after trying many solutions is to use setAttributedTitle instead of setTitle.

To make the title string for AttributedString:

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@" new Test "];

[button setAttributedTitle:attString forState:UIControlStateNormal];

By the way, this problem is not occasionally happen in normal but it suddenly happen mainly for the following reasons:

  1. If you change the enabled state of a UIButton and try to change the title.

  2. If you use an attributed value and then want to change it using setTitle, the attributed value is superior to the title in that case.

  3. If you navigate to another view controller and then return back to update the button title.

Upvotes: 1

Pulkit Arora
Pulkit Arora

Reputation: 61

Change the button type to 'Custom', instead of 'System' and it will work as expected :)

Upvotes: 4

Moose
Moose

Reputation: 2737

Try to declare you button as a property, either in the interface or in the implementation part of you view / view controller

@property(nonatomic,strong) UIButton* myButton;

then create it

self.myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[_myButton setBackgroundColor:[UIColor blackColor]];
...

It should work. Don't ask me why really - I just think it's all about ARC / Modern memory management..

EDIT:

It should also work if you keep a reference on the button in your implementation..

@implementation myViewController
{
    UIButton* myButton;
}

-(void)createButton
{
    myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
    [myButton setBackgroundColor:[UIColor blackColor]];
    ...
}

...

Upvotes: 0

Siddhesh Mahadeshwar
Siddhesh Mahadeshwar

Reputation: 1611

In my case I tried everything but nothing was working. Then I just changed button type from system to custom from storyboard. BOOM! everything started behaving normally.

Upvotes: 4

dobranoc
dobranoc

Reputation: 475

This may be trivial, but if you set some button image (instead of background image) which fills the whole button frame, this will shift the title right and thus make it invisible. If so, try changing button image to background image.

Upvotes: 2

bachman
bachman

Reputation: 690

Please see if this might help you...when the button is clicked check for condition if buttonToggled...like below when you have a function like changeButtonText

-(IBAction)changeButtonText:(id)sender {
    if (!buttonToggled) {
        [sender setTitle:@"Initial Text" forState:UIControlStateNormal];
        buttonToggled = YES;
    } else {
        [sender setTitle:@"Alternative Text" forState:UIControlStateNormal];
        buttonToggled = NO;
    }
}

Upvotes: 8

ccoroom
ccoroom

Reputation: 689

In iOS 7, UIButton's title is not updated when it is disabled. It seems like a bug on iOS 7.

As a workaround, update both normal and disabled title. It works!

[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateDisabled];

Upvotes: 5

user3000868
user3000868

Reputation: 455

I had a similar problem using storyboards. Using the answers above I had to use

[mybutton setTitle:@"SomeText" forState:UIControlStateNormal];
[button setNeedsLayout];
[button layoutIfNeeded];

AND I had to make sure that the button type was 'Custom' not 'System' in the attributes inspector.

Upvotes: 12

iOSGuest
iOSGuest

Reputation: 61

It could be the button layout refresh issue..... Try using...

[button setNeedsLayout];
[button layoutIfNeeded];

It will force button to update the layout.

Upvotes: 3

PPVZ
PPVZ

Reputation: 17

Always do addSubview before setting the text on UIButton.

Upvotes: -1

Jeffrey Sun
Jeffrey Sun

Reputation: 8049

I was having the same problem, and I noticed that everywhere else I was setting the attributedTitle. So any updates to the title were not affecting the attributed title.

My solution:

[button setAttributedTitle:@"" forState:UIControlStateNormal];

instead of

[button setTitle:@"" forState:UIControlStateNormal];

Upvotes: 29

StephenDonaldHuffPhD
StephenDonaldHuffPhD

Reputation: 958

Per Apple developer guide, you should not set either the button title label text or color directly as a property (for example, do not do this: myButton.titleLabel.text=@"SomeText" or myButton.titleLabel.textColor=[UIColor blackColor]).

Rather, you set them using the UIButton setter functionality, like this:

    [myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

...or like this:

    [myButton setTitle:@"SomeText" forState:UIControlStateNormal];.

See the Apple guide.

Upvotes: 1

Lukasz
Lukasz

Reputation: 19916

At the moment, the shortest work around I came with is calling:

[button setNeedsLayout];

After updating the title.

This seems to happen in iOS 7.1. All my buttons, which were behaving correctly in previous iOS versions (or maybe just compiled with previous SDKs) suddenly stopped doing that when compiled in Xcode 5.1.1 SDK 7.1.

Looks like Apple's bug.

Upvotes: 21

Kiko Seijo
Kiko Seijo

Reputation: 31

Well, is all about the enabled state of a UIButton, Apple has changed something in 7.1 that does not allow you to change the title if you have the UIButton on a disabled state, thats all.

Thanks Apple, i have lost 10 min. debuging an app that was working fine.

Just found out this morning, got updated XCode yesterday to the 5.1.1 and iOS to 7.1

Upvotes: 3

dragonflyesque
dragonflyesque

Reputation: 485

This is kinda late and somewhat relates to Walter Schurter's response:

I had a similar issue where my button text was being set correctly until I updated to 7.1. Then I found that since my button was disabled, I had to set the title color and title text for the disabled state to get the text to show up. Then everything worked like a charm!

Upvotes: 5

Walter Schurter
Walter Schurter

Reputation: 1017

This worked for me to update the title text (iOS 7.1, Xcode 5.1):

button.enabled = FALSE;
[button setTitle:@"Test" forState:UIControlStateNormal];
button.enabled = TRUE;

Upvotes: 52

Nikos M.
Nikos M.

Reputation: 13783

Try this:

[button setTitle:@"Bye" forState:UIControlStateHighlighted];

or:

[button setTitle:@"Bye" forState:UIControlStateSelected];

You could also modify your void function:

-(void) userClicked
{
    NSLog(@"USER CLICKED!!!");
    if ([NSThread isMainThread])
    {
        NSLog(@"is main thread");
    }

    [myButton setTitle:@"Bye" forState:UIControlStateNormal];
}

Upvotes: 4

neutrino
neutrino

Reputation: 2317

Finally, I've figured it out. There were two problems:

1) button was not in state UIControlStateNormal.

2) I was calling a method performing a long computation just after setting the title, [self someLengthyComputation].

I've solved the problem by:

1) Setting the title for all states of the button.

2) Performing that big computation in another thread, not the main thread.

My working code now is:

-(void) userClicked:(UIButton*)button
{
    NSLog(@"USER CLICKED!!!");
    if ([NSThread isMainThread])
    {
        NSLog(@"is main thread");
    }

    [button setTitle:@"Bye" forState:UIControlStateHighlighted];
    [button setTitle:@"Bye" forState:UIControlStateNormal];
    [button setTitle:@"Bye" forState:UIControlStateSelected];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [self someLengthyComputation];
    });
}

Thank you very much to everybody who has answered/commented!

Upvotes: 2

Eugene Tartakovsky
Eugene Tartakovsky

Reputation: 1604

There are several issues in your code:

You are assigning callback to the button:

@selector(userClicked:)

but your code is in another method:

-(void)userTapOnTapToRefreshView:(UIButton*)button

To fix that you need to implement something like this:

-(void)userClicked:(id)sender
{
    [(UIButton*)sender setTitle:@"Bye" forState:UIControlStateNormal];
}

Also this part of code does not make sense for me:

[parentView myButton];

Try to change it to:

[parentView addSubview:myButton];

Hope it will help :)

Upvotes: 6

Hunter
Hunter

Reputation: 136

I doubt if 'button' passed in as a parameter is myButton.Anyway,if myButton is a member var,you can just [myButton setTitle:@"Bye" forState:UIControlStateNormal];

Upvotes: 0

Related Questions