Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an action to a UIAlertView button using Swift iOS

I want to add another button other than the "OK" button which should just dismiss the alert. I want the other button to call a certain function.

var logInErrorAlert: UIAlertView = UIAlertView() logInErrorAlert.title = "Ooops" logInErrorAlert.message = "Unable to log in." logInErrorAlert.addButtonWithTitle("Ok") 

How do I add another button to this alert, and then allow it to call a function once clicks so lets say we want the new button to call:

 retry() 
like image 219
Stephen Fox Avatar asked Jun 12 '14 23:06

Stephen Fox


People also ask

How do I show alert pop up in Swift?

Enter Swift as Language and choose Next. Go to the Storyboard and drag a Button to the main View. Double-click the button and give it a title of "Display Alert". Select the button and click the Pin Auto Layout button on the bottom-right of the Storyboard.


2 Answers

The Swifty way is to use the new UIAlertController and closures:

    // Create the alert controller     let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)      // Create the actions     let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {         UIAlertAction in         NSLog("OK Pressed")     }     let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {         UIAlertAction in         NSLog("Cancel Pressed")     }      // Add the actions     alertController.addAction(okAction)     alertController.addAction(cancelAction)      // Present the controller     self.presentViewController(alertController, animated: true, completion: nil) 

Swift 3:

    // Create the alert controller     let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)      // Create the actions     let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {         UIAlertAction in         NSLog("OK Pressed")     }     let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {         UIAlertAction in         NSLog("Cancel Pressed")     }      // Add the actions     alertController.addAction(okAction)     alertController.addAction(cancelAction)      // Present the controller     self.present(alertController, animated: true, completion: nil) 
like image 51
Jake Avatar answered Oct 10 '22 02:10

Jake


func showAlertAction(title: String, message: String){     let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: {(action:UIAlertAction!) in         print("Action")     }))     alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: nil))     self.present(alert, animated: true, completion: nil) } 
like image 24
Chetan Avatar answered Oct 10 '22 00:10

Chetan