2014-07-04 18 views
6

Sono programmatore di livello principianti. Sto cercando di aggiungere azioni ai pulsanti su Alert, ma non funziona. Voglio solo verificare se la scelta del pulsante di avviso può modificare il testo nell'etichetta, ma non funziona. Ovviamente riesco a vedere bene l'avviso e i pulsanti, ma non succede nulla dopo aver fatto clic sul pulsante.Come aggiungere un'azione ai pulsanti in Alert in Swift

@IBOutlet var statusLabelAlert : UILabel 

var alertTest = UIAlertView() 

@IBAction func alertButton(sender : AnyObject) { 

    alertTest.message = "Select one!" 
    alertTest.addButtonWithTitle("1st") 
    alertTest.addButtonWithTitle("2nd") 
    alertTest.addButtonWithTitle("3rd") 
    alertTest.title = "Test Alert" 
    alertTest.show() 

} 
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ 
    switch buttonIndex{ 
    case 0: 
     statusLabelAlert.text = "1st" 
    case 1: 
     statusLabelAlert.text = "2nd" 
    case 2: 
     statusLabelAlert.text = "3rd" 
    default: 
     statusLabelAlert.text = "error" 
    } 

} 

risposta

8

Il tuo metodo ClickedButtonAtIndex chiama? Metti un punto di interruzione e fai il debug del codice. Non si imposta il delegato di alertView.

@IBOutlet var statusLabelAlert : UILabel 

var alertTest = UIAlertView() 
alertTest.delegate = self //set the delegate of alertView 

@IBAction func alertButton(sender : AnyObject) { 

alertTest.message = "Select one!" 
alertTest.addButtonWithTitle("1st") 
alertTest.addButtonWithTitle("2nd") 
alertTest.addButtonWithTitle("3rd") 
alertTest.title = "Test Alert" 
alertTest.show() 

} 
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ 
switch buttonIndex{ 
case 0: 
    statusLabelAlert.text = "1st" 
case 1: 
    statusLabelAlert.text = "2nd" 
case 2: 
    statusLabelAlert.text = "3rd" 
default: 
    statusLabelAlert.text = "error" 
} 

} 
3

è necessario impostare il delegato della vista avviso:

alertTest.delegate = self 

UIAlertView utilizza il delegate pattern che significa che chiama i metodi su un suo delegato per ottenere certe cose o di notificare degli eventi. Con UIAlertView, uno di quegli eventi è quando l'utente tocca un pulsante. Per la vista di avviso per sapere chi dire sul tocco di un utente, è necessario specificare un delegato che implementa il protocollo UIAlertViewDelegate. Il tuo codice implementa il protocollo ma non dice mai all'avviso che vuoi essere il suo delegato in modo da non ricevere mai la chiamata al metodo.

1

Se questo è per iOS 8, è necessario passare a UIAlertController e UIAlertAction. UIAlertAction contiene ciò che il pulsante dovrebbe fare.

3
@IBOutlet var statusLabelAlert : UILabel 
var alertTest = UIAlertView() 
@IBAction func alertButton(sender : AnyObject) 
{ 
    alertTest.delegate = self 
    alertTest.message = "Select one!" 
    alertTest.addButtonWithTitle("1st") 
    alertTest.addButtonWithTitle("2nd") 
    alertTest.addButtonWithTitle("3rd") 
    alertTest.title = "Test Alert" 
    alertTest.show() 

} 

func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) 
{ 
    switch buttonIndex 
    { 
     case 0: 
     statusLabelAlert.text = "1st" 
     case 1: 
     statusLabelAlert.text = "2nd" 
     case 2: 
     statusLabelAlert.text = "3rd" 
     default: 
     statusLabelAlert.text = "error" 
    } 
} 
1
IBOutlet var statusLabelAlert : UILabel 

@IBAction func alertButton(sender : AnyObject) { 

let alert = UIAlertController(title: "Alert", message: "This is an Alert", preferredStyle: UIAlertControllerStyle.Alert) 

alert.addAction(UIAlertAction(title: "1st", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in 
statusLabelAlert.text = "1st" 
})) 

alert.addAction(UIAlertAction(title: "2nd", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in 
statusLabelAlert.text = "2nd" 
})) 

self.presentViewController(alert, animated: true, completion: nil) 

} 

Se non si vuole fare qc quando si preme il pulsante:

alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil)) 
Problemi correlati