9

IBOutletCollectionのタイトルを動的に更新しようとしていますUIButton。タイトルが設定されることを期待しています

  • 選択時の文字「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
4

3 に答える 3

20

attributedTitleの代わりにIB が設定されているため、機能していません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 番目のオプションでは、書式設定は保持されません。)

于 2013-11-26T20:03:42.793 に答える
1

いろいろ試してみましたが、うまくいく唯一の方法は以下のとおりです。ただし、これはCスタイルのロジックであり、選択および無効化されたUIButtonコントロール状態の意味を変更します。間違いなくハック:(

//        [cardButton setTitle:card.contents
//                    forState:UIControlStateSelected|UIControlStateDisabled];
if(cardButton.selected && !cardButton.enabled) {
    [cardButton setTitle:card.contents forState:UIControlStateNormal];
}
于 2013-02-22T18:12:11.573 に答える
0
[button setTitle:@"S" forState:UIControlStateSelected];
        [button setTitle:@"D|S" forState:UIControlStateSelected|UIControlStateDisabled];

2つの場合のsetTitleはUIControlStateSelected、コンパイラを混乱させます。両方の条件を同時に実行する可能性があります。2行目のコードを変更してみてください。ハッピーコーディングを

于 2013-02-22T05:23:14.063 に答える