-
-
Save elenamene/f567dc2da07fd743a41523fb883993ef to your computer and use it in GitHub Desktop.
Swift 4 Alert Controller Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Updated for Swift 4 | |
//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: UIAlertControllerStyle.alert) | |
// add an action (button) | |
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.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: UIAlertControllerStyle.alert) | |
// add the actions (buttons) | |
alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.default, handler: nil)) | |
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.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: UIAlertControllerStyle.alert) | |
// add the actions (buttons) | |
alert.addAction(UIAlertAction(title: "Remind Me Tomorrow", style: UIAlertActionStyle.default, handler: nil)) | |
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)) | |
alert.addAction(UIAlertAction(title: "Launch the Missile", style: UIAlertActionStyle.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: UIAlertActionStyle.destructive, handler: { action in | |
// do something like... | |
self.launchMissile() | |
})) | |
//Notes | |
//Multiple buttons do not necessarily need to use different UIAlertActionStyle types. They could all be .default. | |
//For more than three buttons consider using an Action Sheet. | |
//Alert with Text Box | |
class ViewController: UIViewController { | |
@IBAction func showAlertButtonTapped(_ sender: UIButton) { | |
print("Add Note!") | |
//1. Create the alert controller. | |
let alert = UIAlertController(title: "Title", message: "Enter some text", preferredStyle: .alert) | |
//2. Add the text field. You can configure it however you need. | |
alert.addTextField { (textField) in | |
textField.text = "Some default text..." | |
textField.placeholder = "Enter Text Here" | |
} | |
// 3. Grab the value from the text field, and print it when the user clicks OK. | |
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in | |
let textField = alert?.textFields![0] // Force unwrapping because we know it exists. | |
print("Text field: \(textField?.text ?? "")") | |
})) | |
// 4. Present the alert. | |
self.present(alert, animated: true, completion: nil) | |
} | |
//4 Buttons | |
class ViewController: UIViewController { | |
@IBAction func showAlertButtonTapped(_ sender: UIButton) { | |
let alert = UIAlertController(title: "", | |
message: "", | |
preferredStyle: .alert) | |
// Change font of the title and message | |
let titleFont:[String : AnyObject] = [ NSFontAttributeName : UIFont(name: "AmericanTypewriter", size: 18)! ] | |
let messageFont:[String : AnyObject] = [ NSFontAttributeName : UIFont(name: "HelveticaNeue-Thin", size: 14)! ] | |
let attributedTitle = NSMutableAttributedString(string: "Multiple buttons", attributes: titleFont) | |
let attributedMessage = NSMutableAttributedString(string: "Select an Action", attributes: messageFont) | |
alert.setValue(attributedTitle, forKey: "attributedTitle") | |
alert.setValue(attributedMessage, forKey: "attributedMessage") | |
let action1 = UIAlertAction(title: "Action 1", style: .default, handler: { (action) -> Void in | |
print("ACTION 1 selected!") | |
}) | |
let action2 = UIAlertAction(title: "Action 2", style: .default, handler: { (action) -> Void in | |
print("ACTION 2 selected!") | |
}) | |
let action3 = UIAlertAction(title: "Action 3", style: .default, handler: { (action) -> Void in | |
print("ACTION 3 selected!") | |
}) | |
// Cancel button | |
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) -> Void in }) | |
// Restyle the view of the Alert | |
alert.view.tintColor = UIColor.brown // change text color of the buttons | |
alert.view.backgroundColor = UIColor.cyan // change background color | |
alert.view.layer.cornerRadius = 25 // change corner radius | |
// Add action buttons and present the Alert | |
alert.addAction(action1) | |
alert.addAction(action2) | |
alert.addAction(action3) | |
alert.addAction(cancel) | |
present(alert, animated: true, completion: nil) | |
} | |
} | |
//Delete with "Yes and No" + Plus Go Back | |
class ViewController: UIViewController { | |
@IBAction func showAlertButtonTapped(_ sender: UIButton) { | |
//Delete | |
let alert = UIAlertController(title: "Delete Contact", message: "Are you sure you want to delete this contact?", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Yes", style: .destructive, handler: { [] (_) in | |
print("Deleting...") | |
_ = self.navigationController?.popViewController(animated: true) | |
})) | |
alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil)) | |
self.present(alert, animated: true, completion: nil) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment