2013-02-22 8 views
9

Tôi đang cố gắng tự động cập nhật tiêu đề của số IBOutletCollection trong số UIButton s. Tôi hy vọng danh hiệu được thiết lập đểUIButton -setTitle: forState: dường như không hoạt động

  • chữ 'S' khi lựa chọn và
  • dòng chữ "D | S" khi bị vô hiệu và được chọn.

Nó không hoạt động, vì vậy tôi in ra các titleForState: s và có vẻ như tiêu đề không được đặt đúng cách. Tôi có đang sử dụng setTitle: forState: chính xác không?

@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); 
    } 
} 

Dưới đây là giao diện điều khiển đầu ra:

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 

Trả lời

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

setTitle cho UIControlStateSelected trong hai trường hợp làm cho trình biên dịch nhầm lẫn. Có một cơ hội để thực hiện cả hai điều kiện cùng một lúc. Hãy thử thay đổi mã trong dòng thứ hai..Có một Mã số hạnh phúc

+0

Tôi không thấy bất kỳ cách nào khác trong tham chiếu. Có phương pháp nào khác để làm điều đó không? – ydmm

1

Sau khi thử nhiều thứ khác nhau, cách duy nhất tôi nhận được nó hoạt động như sau. Nhưng đây là một logic kiểu C và thay đổi ý nghĩa của trạng thái điều khiển UIButton đã chọn và vô hiệu hóa. Chắc chắn một hack :(

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

Nó không làm việc vì IB đặt attributedTitle thay vì title

Hãy thử điều này thay vì:.

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

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

Hoặc cách khác:

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

(Tùy chọn thứ hai sẽ không giữ nguyên định dạng của bạn.)