Created
February 19, 2017 11:05
-
-
Save abarrak/eae35721ea30aa04ac912d6dd00ffc30 to your computer and use it in GitHub Desktop.
Alert snippet in 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
| // | |
| // ViewControllerExtension.swift | |
| // VirtualTourist | |
| // | |
| // Created by Abdullah on 2/15/17. | |
| // | |
| import UIKit | |
| extension UIViewController { | |
| // Utility for informational alert | |
| func alertMessage(_ title: String, message: String, completionHandler: ((UIAlertAction) -> Void)? = nil) { | |
| let alert = UIAlertController(title: title, | |
| message: message, | |
| preferredStyle: UIAlertControllerStyle.alert) | |
| let handler = completionHandler ?? { action in self.dismiss(animated: true, completion: nil) } | |
| let okAction = UIAlertAction(title: "OK", | |
| style: UIAlertActionStyle.default, | |
| handler: handler) | |
| alert.addAction(okAction) | |
| present(alert, animated: true, completion: nil) | |
| } | |
| // Utility for alert with question | |
| func alertQuestion(_ title: String, message: String, okHandler: @escaping (_ action: UIAlertAction?) -> Void) { | |
| let alert = UIAlertController(title: title, message:message, | |
| preferredStyle: UIAlertControllerStyle.alert) | |
| alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: okHandler)) | |
| alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in | |
| self.dismiss(animated: true, completion: nil) | |
| })) | |
| present(alert, animated: true, completion: nil) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment