UIButton -setTitle: forState: doesn't seem to work

I'm trying to dynamically update the title of an IBOutletCollection of UIButtons. I expect the title to be set to

  • the letter 'S' when selected and
  • the text "D|S" when disabled and selected.

It wasn't working, so I printed out the titleForState:s and it looks like the title is not getting set properly. Am I using setTitle: forState: correctly?

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *buttons;
...
- (void)updateUI  // Calling this from IBAction
{
    for(UIButton *button in self.buttons) {
        [button setTitle:@"S" forState:UIControlStateSelected];
        [button setTitle:@"D|S" forState:UIControlStateSelected|UIControlStateDisabled];

        NSLog(@"%@ %@ %@ %@ %d %d",
              [button titleForState:UIControlStateSelected],
              [button titleForState:UIControlStateSelected],
              [button titleForState:UIControlStateNormal],
              [button titleForState:UIControlStateSelected|UIControlStateDisabled],
              button.selected,
              button.enabled);
    }
}

Here's the console output:

2013-02-21 21:05:36.070 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.072 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.073 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.073 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.073 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.074 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.074 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.074 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.075 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.075 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.076 Buttons[37130:c07] D|S D|S   0 1
2013-02-21 21:05:36.076 Buttons[37130:c07] D|S D|S   0 1

Answers


It's not working because IB sets attributedTitle instead of title.

Try this instead:

NSAttributedString *attributedTitle = [self.myButton attributedTitleForState:UIControlStateNormal];
NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithAttributedString:attributedTitle];
[mas.mutableString setString:@"New Text"];

[self.myButton setAttributedTitle:mas forState:UIControlStateNormal];

Or, alternatively:

[self.myButton setAttributedTitle:nil forState:UIControlStateNormal];
[self.myButton setTitle:@"New Text" forState:UIControlStateNormal];

(The second option won't preserve your formatting.)


After trying a lot of different things, the only way I got it working is as below. But this is a C-style logic and changes the meaning of selected and disabled UIButton control state. Definitely a hack :(

//        [cardButton setTitle:card.contents
//                    forState:UIControlStateSelected|UIControlStateDisabled];
if(cardButton.selected && !cardButton.enabled) {
    [cardButton setTitle:card.contents forState:UIControlStateNormal];
}

[button setTitle:@"S" forState:UIControlStateSelected];
        [button setTitle:@"D|S" forState:UIControlStateSelected|UIControlStateDisabled];

setTitle for UIControlStateSelected in two cases make the compiler confuse. There is a chance to execute both the condition at once. Try to change the code in second line..Have a Happy Coding


Need Your Help

iOS 7 BUG - NSAttributedString does not appear

ios7 xcode5 nsattributedstring

Last week I asked a question about a Simulator bug with NSAttributedString not displaying: iOS 7 Simulator Bug - NSAttributedString does not appear

SQL Server's READ COMMITTED SNAPSHOT vs SNAPSHOT

sql-server sql-server-2012

I was researching the differences between SQL Server's READ COMMITTED SNAPSHOT and SNAPSHOT isolation levels and came across the following resource: