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