2014-07-04 13 views
6

Jestem programistą na poziomie początkującym. Próbuję dodać akcję do przycisków w Alert, ale to nie działa. Chcę tylko sprawdzić, czy wybór przycisku alertu może zmienić tekst w etykiecie, ale nie działa. Oczywiście dobrze widzę alarm i przyciski, ale po kliknięciu przycisku nic się nie dzieje.Jak dodać akcję do przycisków w Alertu w 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" 
    } 

} 

Odpowiedz

8

Czy Twoja metoda clickedButtonAtIndex wywołuje? Umieść punkt przerwania i debuguj kod. Nie ustawiasz delegata 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

Musisz ustawić delegata alertu Widok:

alertTest.delegate = self 

UIAlertView używa delegate pattern co oznacza, że ​​wywołuje metody na jego delegata, aby osiągnąć pewne rzeczy lub powiadamiania o zdarzeniach. W przypadku UIAlertView jedno z tych zdarzeń ma miejsce, gdy użytkownik dotknie przycisku. Aby wyświetlić widok alertów, aby wiedzieć, komu powiedzieć o kliknięciu użytkownika, należy określić delegata, który implementuje protokół UIAlertViewDelegate. Twój kod implementuje protokół, ale nigdy nie informuje go, że chcesz być jego delegatem, więc nigdy nie otrzymasz wywołania metody.

1

Jeśli dotyczy to iOS 8, należy przełączyć na UIAlertController i UIAlertAction. UIAlertAction zawiera to, co powinien zrobić przycisk.

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) 

} 

Jeśli nie chcesz robić czegoś, gdy przycisk jest wciśnięty:

alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil)) 
Powiązane problemy