2013-02-22 16 views
9

Sto tentando di aggiornare dinamicamente il titolo di uno IBOutletCollection di UIButton s. Mi aspetto che il titolo per essere impostato suUIButton -setTitle: forState: sembra non funzionare

  • la lettera 'S' quando selezionato e
  • il testo "D | S" quando disattivato e selezionato.

Non funzionava, quindi ho stampato il titleForState: e sembra che il titolo non sia impostato correttamente. Sto usando setTitle: forState: correttamente?

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

Ecco l'output della console:

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 

risposta

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

setTitle per UIControlStateSelected in due casi fanno il confondere compilatore. C'è la possibilità di eseguire entrambe le condizioni contemporaneamente. Prova a cambiare il codice in seconda riga .. Avere un Happy Coding

+0

Non vedo nessun altro modo nel riferimento. C'è qualche altro metodo per farlo? – ydmm

1

Dopo aver provato un sacco di cose diverse, l'unico modo in cui l'ho fatto funzionare è il seguente. Ma questa è una logica in stile C e cambia il significato dello stato di controllo UIButton selezionato e disabilitato. Sicuramente un hack :(

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

Non funziona perché IB imposta attributedTitle invece di title

Prova a modificare:.

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

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

O, in alternativa:

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

(La seconda opzione non manterrà la formattazione.)

Problemi correlati