How to add action to buttons in Alert in Swift

I am a beginner programmer. I am trying to add an action to buttons in Alert, but this will not work. I just want to check if the warning button button can change the text in the shortcut, but it does not work. Of course, I can clearly see the notification and buttons, but nothing happens after clicking the button.

@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" } } 
+6
source share
5 answers

Is the clickedButtonAtIndex method called? Place a breakpoint and debug the code. You do not set the alertView delegate.

 @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" } } 
+8
source

You must set the alert presentation delegate:

 alertTest.delegate = self 

UIAlertView uses a delegate template , which means that it invokes methods on its delegate to achieve certain things or to notify about events. With UIAlertView one of these events is when the user clicks a button. To view warnings about who should talk about user clicks, you must specify a delegate that implements the UIAlertViewDelegate protocol. Your code implements the protocol, but it never reports an alert that you want to be its delegate so you never receive a method call.

+3
source
 @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" } } 
+3
source

If this is for iOS 8, you should switch to UIAlertController and UIAlertAction. UIAlertAction contains what the button should do.

+1
source
 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) } 

If you do not want to do sth when the button is clicked:

 alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil)) 
+1
source

Source: https://habr.com/ru/post/971781/


All Articles