Risposte:
Ehi Namratha, se stai chiedendo di cambiare il testo e lo stato abilitato / disabilitato di un pulsante UIB, può essere fatto abbastanza facilmente come segue;
[myButton setTitle:@"Normal State Title" forState:UIControlStateNormal]; // To set the title
[myButton setEnabled:NO]; // To toggle enabled / disabled
Se hai creato i pulsanti in Interface Builder e desideri accedervi in codice, puoi sfruttare il fatto che vengono passati come argomento alle IBActionchiamate:
- (IBAction) triggerActionWithSender: (id) sender;
Questo può essere associato al pulsante e otterrai il pulsante sendernell'argomento quando l'azione viene attivata. Se ciò non è sufficiente (perché è necessario accedere ai pulsanti da qualche altra parte rispetto alle azioni), dichiara un'uscita per il pulsante:
@property(retain) IBOutlet UIButton *someButton;
Quindi è possibile associare il pulsante in IB al controller, il codice di caricamento NIB imposterà il valore della proprietà durante il caricamento dell'interfaccia.
[myButton setTitle: @"myTitle" forState: UIControlStateNormal];
Utilizzare UIControlStateNormalper impostare il titolo.
Ci sono un paio di stati forniti dai pulsanti UI, puoi dare un'occhiata:
[myButton setTitle: @"myTitle" forState: UIControlStateApplication];
[myButton setTitle: @"myTitle" forState: UIControlStateHighlighted];
[myButton setTitle: @"myTitle" forState: UIControlStateReserved];
[myButton setTitle: @"myTitle" forState: UIControlStateSelected];
[myButton setTitle: @"myTitle" forState: UIControlStateDisabled];
Se qualcuno, che sta cercando una soluzione in Swift, atterrasse qui, sarebbe:
myButton.isEnabled = false // disables
myButton.setTitle("myTitle", for: .normal) // sets text
Documentazione: isEnabled , setTitle .
Codice precedente:
myButton.enabled = false // disables
myButton.setTitle("myTitle", forState: UIControlState.Normal) // sets text
Per cambiare il titolo del pulsante:
[mybtn setTitle:@"My Button" forState:UIControlStateNormal];
[mybtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Per disabilitare:
[mybtn setEnabled:NO];
In Swift 3, puoi semplicemente cambiare il titolo di un pulsante:
button.setTitle("Title", for: .normal)
e disabiliti il pulsante:
button.isEnabled = false
.normalè uguale a UIControlState.normalperché il tipo è dedotto.
SWIFT 4 con estensione
impostato:
// set button label for all states
extension UIButton {
public func setAllStatesTitle(_ newTitle: String){
self.setTitle(newTitle, for: .normal)
self.setTitle(newTitle, for: .selected)
self.setTitle(newTitle, for: .disabled)
}
}
e usa:
yourBtn.setAllStatesTitle("btn title")
Se si desidera modificare il titolo in risposta a un tocco, è possibile provare questo all'interno del metodo IBAction del pulsante nel delegato del controller di visualizzazione. Questo attiva e disattiva una chat vocale. L'impostazione della chat vocale non è trattata qui!
- (IBAction)startChat:(id)sender {
UIButton *chatButton = (UIButton*)sender;
if (!voiceChat.active) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Voice Chat"
message:@"Voice Chat will become live. Please be careful with feedback if your friend is nearby."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
[voiceChat start];
voiceChat.active = YES;
[chatButton setTitle:@"Stop Chat" forState:UIControlStateNormal];
}
else {
[voiceChat stop];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Voice Chat"
message:@"Voice Chat is closed"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
voiceChat.active = NO;
[chatButton setTitle:@"Chat" forState:UIControlStateNormal];
}
}
voiceChat è specifico per la chat vocale ovviamente, ma puoi utilizzare la tua proprietà booleana locale per controllare l'interruttore.