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

Posted

技术标签:

【中文标题】UIButton -setTitle: forState: 似乎不起作用【英文标题】:UIButton -setTitle: forState: doesn't seem to work 【发布时间】:2013-02-22 05:19:01 【问题描述】:

我正在尝试动态更新IBOutletCollectionUIButtons 的标题。我希望将标题设置为

选中时的字母“S” 禁用和选中文本“D|S”。

它不起作用,所以我打印了titleForState: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);
    

这是控制台输出:

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

【问题讨论】:

【参考方案1】:

它不起作用,因为 IB 设置了 attributedTitle 而不是 title

试试这个:

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

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

或者,或者:

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

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

【讨论】:

【参考方案2】:

在尝试了很多不同的事情之后,我让它工作的唯一方法如下。但这是一个 C 风格的逻辑,改变了 UIButton 控件状态的选中和禁用的含义。绝对是一个黑客:(

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

【讨论】:

【参考方案3】:
[button setTitle:@"S" forState:UIControlStateSelected];
        [button setTitle:@"D|S" forState:UIControlStateSelected|UIControlStateDisabled];

setTitle for UIControlStateSelected 在两种情况下会使编译器感到困惑。有机会同时执行这两个条件。尝试更改第二行中的代码..祝您编码愉快

【讨论】:

我在参考中没有看到任何其他方式。有没有其他方法可以做到?

以上是关于UIButton -setTitle: forState: 似乎不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何将部分更新操作从 ObjectContext 转换为 DbContext

非接触式智能卡仅响应第一个 APDU 命令

从 UIButton 中获取 UIButton 的标签,该 UIButton 位于此 UIButton 呈现的弹出框中

如何删除多个 UIButton 或动态创建的特定 UIButton?

为啥 Swift4 会强制转换 UIButton 数组!到 [UIButton?] 类型?

UIButton:根据titleLabel Text计算UIButton的高度