Swift alert view with OK and Cancel: which button tapped? Swift alert view with OK and Cancel: which button tapped? xcode xcode

Swift alert view with OK and Cancel: which button tapped?


If you are using iOS8, you should be using UIAlertController — UIAlertView is deprecated.

Here is an example of how to use it:

var refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.Alert)refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in  print("Handle Ok logic here")  }))refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in  print("Handle Cancel Logic here")  }))presentViewController(refreshAlert, animated: true, completion: nil)

As you can see the block handlers for the UIAlertAction handle the button presses. A great tutorial is here (although this tutorial is not written using swift):http://hayageek.com/uialertcontroller-example-ios/

Swift 3 update:

let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.alert)refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in    print("Handle Ok logic here")}))refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in    print("Handle Cancel Logic here")}))present(refreshAlert, animated: true, completion: nil)

Swift 5 update:

let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.alert)refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in      print("Handle Ok logic here")}))refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in      print("Handle Cancel Logic here")}))present(refreshAlert, animated: true, completion: nil)

Swift 5.3 update:

let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertController.Style.alert)refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in      print("Handle Ok logic here")}))refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in      print("Handle Cancel Logic here")}))present(refreshAlert, animated: true, completion: nil)


var refreshAlert = UIAlertController(title: "Log Out", message: "Are You Sure to Log Out ? ", preferredStyle: UIAlertControllerStyle.Alert)refreshAlert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in    self.navigationController?.popToRootViewControllerAnimated(true)}))refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in    refreshAlert .dismissViewControllerAnimated(true, completion: nil)}))presentViewController(refreshAlert, animated: true, completion: nil)


You can easily do this by using UIAlertController

let alertController = UIAlertController(       title: "Your title", message: "Your message", preferredStyle: .alert)let defaultAction = UIAlertAction(       title: "Close Alert", style: .default, handler: nil)//you can add custom actions as well alertController.addAction(defaultAction)present(alertController, animated: true, completion: nil)

.

Reference: iOS Show Alert