Dogahe
Dogahe

Reputation: 1430

Toggling the UILabel text on a UIButton programmatically

I created the following outlet

IBOutlet UIButton *oneButton;

and the following method when a button is pressed:

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = sender.titleLabel.text;
    if ([digit isEqualToString:@"1"])
    {
        oneButton.titleLabel.text = @"11";
    } else if ([digit isEqualToString:@"11"])
    {
        oneButton.titleLabel.text = @"1";
    }
}

So basically I want to toggle the title of the button between 1 and 11, whenever pressed. By putting breakpoint, I see that the program goes through the line oneButton.titleLabel.text = @"11"; but the title never actually changes. oneButton is connected to the file's owner in the IB.

Upvotes: 1

Views: 370

Answers (1)

BP.
BP.

Reputation: 10083

Try something like this instead:

[oneButton setTitle:@"11" forState:UIControlStateNormal];

Upvotes: 2

Related Questions