How to add action to UIAlertView button using Swift iOS

I want to add another button besides the OK button, which should just close the warning. I want another button to call a specific function.

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

How to add another button to this warning, and then allow it to call the function after a click, so let's say that we want the new button to call:

  retry() 
+38
ios swift uialertview
Jun 12 '14 at 23:11
source share
7 answers

Swifty's 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) 
+152
Jun 12 '14 at 23:46
source share
 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) } 
+13
03 Feb '16 at 12:53 on
source share

UIAlertViews use a delegate to contact you, the client.

You add a second button, and you create an object to receive delegate messages from the view:

 class LogInErrorDelegate : UIAlertViewDelegate { init {} // not sure of the prototype of this, you should look it up func alertView(view :UIAlertView, clickedButtonAtIndex :Integer) -> Void { switch clickedButtonAtIndex { case 0: userClickedOK() // er something case 1: userClickedRetry() /* Don't use "retry" as a function name, it a reserved word */ default: userClickedRetry() } } /* implement rest of the delegate */ } logInErrorAlert.addButtonWithTitle("Retry") var myErrorDelegate = LogInErrorDelegate() logInErrorAlert.delegate = myErrorDelegate 
+7
Jun 12 '14 at 23:39
source share

based on fast:

 let alertCtr = UIAlertController(title:"Title", message:"Message", preferredStyle: .Alert) let Cancel = AlertAction(title:"remove", style: .Default, handler: {(UIAlertAction) -> Void in }) let Remove = UIAlertAction(title:"remove", style: .Destructive, handler:{(UIAlertAction)-> Void inself.colorLabel.hidden = true }) alertCtr.addAction(Cancel) alertCtr.addAction(Remove) self.presentViewController(alertCtr, animated:true, completion:nil)} 
+2
Aug 09 '16 at 22:12
source share

See my code:

  @IBAction func foundclicked(sender: AnyObject) { if (amountTF.text.isEmpty) { let alert = UIAlertView(title: "Oops! Empty Field", message: "Please enter the amount", delegate: nil, cancelButtonTitle: "OK") alert.show() } else { var alertController = UIAlertController(title: "Confirm Bid Amount", message: "Final Bid Amount : "+amountTF.text , preferredStyle: .Alert) var okAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default) { UIAlertAction in JHProgressHUD.sharedHUD.loaderColor = UIColor.redColor() JHProgressHUD.sharedHUD.showInView(self.view, withHeader: "Amount registering" , andFooter: "Loading") } var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { UIAlertAction in alertController .removeFromParentViewController() } alertController.addAction(okAction) alertController.addAction(cancelAction) self.presentViewController(alertController, animated: true, completion: nil) } } 
+1
Aug 24 '15 at 10:28
source share

Swift 3.0 Jake's response version

// Create an alert controller

 let alertController = UIAlertController(title: "Alert!", message: "There is no items for the current user", 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) 
+1
Aug 29 '17 at 10:23 on
source share

Swift 4 Update

  // 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) 
+1
Nov 13 '17 at 10:07 on
source share



All Articles