Created
December 5, 2018 07:11
-
-
Save cumanzor/ac23c592413d709c95e1510c52c32c62 to your computer and use it in GitHub Desktop.
[Show simple UIAlerts from any VC] #ios #swift
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
extension UIViewController { | |
func showAlert(withTitle title: String, message : String, withActions actions:[UIAlertAction] = [], withCompletion c:(()->())?=nil) { | |
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
if actions.isEmpty{ // default ok action | |
let defaultAction = UIAlertAction(title: "Ok", style: .default) | |
alertController.addAction(defaultAction) | |
} | |
for action in actions{ | |
alertController.addAction(action) | |
} | |
self.present(alertController, animated: true, completion: c) | |
} | |
} | |
// use it like this: | |
// show a simple message with an ok action that just dismisses the alert | |
showAlert(withTitle: "Error", message: "Unable to do stuff.") | |
// add an action (pop the VC) | |
let backAction = UIAlertAction(title: "Ok", style: .default) { (action) in | |
self.navigationController?.popViewController(animated: true) | |
} | |
showAlert(withTitle: "Error", message: "Unable to do stuff, please return and try again.", withActions: [backAction]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment