How would I create a UIAlertView in Swift?
From the UIAlertView
class:
// UIAlertView is deprecated. Use UIAlertController with apreferredStyle of UIAlertControllerStyleAlert instead
On iOS 8, you can do this:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))self.presentViewController(alert, animated: true, completion: nil)
Now UIAlertController
is a single class for creating and interacting with what we knew as UIAlertView
s and UIActionSheet
s on iOS 8.
Edit: To handle actions:
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in switch action.style{ case .Default: print("default") case .Cancel: print("cancel") case .Destructive: print("destructive") }}}))
Edit for Swift 3:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))self.present(alert, animated: true, completion: nil)
Edit for Swift 4.x:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: .alert)alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in switch action.style{ case .default: print("default") case .cancel: print("cancel") case .destructive: print("destructive") }}))self.present(alert, animated: true, completion: nil)
One Button
class ViewController: UIViewController { @IBAction func showAlertButtonTapped(_ sender: UIButton) { // create the alert let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: UIAlertController.Style.alert) // add an action (button) alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)) // show the alert self.present(alert, animated: true, completion: nil) }}
Two Buttons
class ViewController: UIViewController { @IBAction func showAlertButtonTapped(_ sender: UIButton) { // create the alert let alert = UIAlertController(title: "UIAlertController", message: "Would you like to continue learning how to use iOS alerts?", preferredStyle: UIAlertController.Style.alert) // add the actions (buttons) alert.addAction(UIAlertAction(title: "Continue", style: UIAlertAction.Style.default, handler: nil)) alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)) // show the alert self.present(alert, animated: true, completion: nil) }}
Three Buttons
class ViewController: UIViewController { @IBAction func showAlertButtonTapped(_ sender: UIButton) { // create the alert let alert = UIAlertController(title: "Notice", message: "Lauching this missile will destroy the entire universe. Is this what you intended to do?", preferredStyle: UIAlertController.Style.alert) // add the actions (buttons) alert.addAction(UIAlertAction(title: "Remind Me Tomorrow", style: UIAlertAction.Style.default, handler: nil)) alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)) alert.addAction(UIAlertAction(title: "Launch the Missile", style: UIAlertAction.Style.destructive, handler: nil)) // show the alert self.present(alert, animated: true, completion: nil) }}
Handling Button Taps
The handler
was nil
in the above examples. You can replace nil
with a closure to do something when the user taps a button. For example:
alert.addAction(UIAlertAction(title: "Launch the Missile", style: UIAlertAction.Style.destructive, handler: { action in // do something like... self.launchMissile()}))
Notes
- Multiple buttons do not necessarily need to use different
UIAlertAction.Style
types. They could all be.default
. - For more than three buttons consider using an Action Sheet. The setup is very similar. Here is an example.
You can create a UIAlert using the standard constructor, but the 'legacy' one seems to not work:
let alert = UIAlertView()alert.title = "Alert"alert.message = "Here's a message"alert.addButtonWithTitle("Understood")alert.show()