简体   繁体   English

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

[英]UIButton -setTitle: forState: doesn't seem to work

I'm trying to dynamically update the title of an IBOutletCollection of UIButton s. 我正在尝试动态更新UIButtonIBOutletCollection的标题。 I expect the title to be set to 我希望标题设置为

  • 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. 它没有用,所以我打印出了titleForState: s,看起来标题没有正确设置。 Am I using setTitle: forState: correctly? 我是否正确使用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

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.) (第二个选项不会保留您的格式。)

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. 但这是一种C风格的逻辑,改变了选择和禁用UIButton控制状态的含义。 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. 两种情况下用于UIControlStateSelected setTitle会使编译器混淆。 There is a chance to execute both the condition at once. 有可能立刻执行这两个条件。 Try to change the code in second line..Have a Happy Coding 尝试更改第二行中的代码。有一个快乐的编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM