UIButton -setTitle: forState:似乎不起作用

时间:2022-07-14 21:08:50

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

我试图动态更新UIButtons的IBOutletCollection的标题。我希望标题被设定为

  • the letter 'S' when selected and
  • 选择和时,字母“S”
  • the text "D|S" when disabled and selected.
  • 当禁用和选择文本“D|S”时。

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?

它不起作用,所以我打印了标题:s,看起来标题没有正确设置。我使用setTitle: forState:正确吗?

@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

3 个解决方案

#1


19  

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

它不起作用,因为IB设置attributedTitle而不是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.)

(第二个选项不会保留格式。)

#2


1  

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 :(

在尝试了很多不同的东西之后,我得到它的唯一方法是如下所示。但这是一个c风格的逻辑,改变了选择和禁用UIButton控件状态的含义。绝对攻击:(

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

#3


0  

[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

在两种情况下选出的uicontrolstatessettitle会让编译器感到困惑。有机会同时执行这两个条件。试着在第二行修改代码。有一个幸福的编码

#1


19  

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

它不起作用,因为IB设置attributedTitle而不是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.)

(第二个选项不会保留格式。)

#2


1  

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 :(

在尝试了很多不同的东西之后,我得到它的唯一方法是如下所示。但这是一个c风格的逻辑,改变了选择和禁用UIButton控件状态的含义。绝对攻击:(

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

#3


0  

[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

在两种情况下选出的uicontrolstatessettitle会让编译器感到困惑。有机会同时执行这两个条件。试着在第二行修改代码。有一个幸福的编码