Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarros35/8b7af5f6eb2469d41e15a57ddbb86951 to your computer and use it in GitHub Desktop.
Save jbarros35/8b7af5f6eb2469d41e15a57ddbb86951 to your computer and use it in GitHub Desktop.
Swift alerts standards
static func showStandardMessage(reference: UIViewController, title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
alert.view.setNeedsLayout()
reference.present(alert, animated: true, completion: nil)
}
static func showStartCancel(reference: UIViewController, title: String, message: String, callback: @escaping (UIAlertAction)->()) {
// create the alert
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Start", style: UIAlertActionStyle.default, handler: callback))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
// show the alert
alert.view.setNeedsLayout()
reference.present(alert, animated: true, completion: nil)
}
// REMARK: yes or no button alert
static func showYesNo(reference: UIViewController, title: String, message: String, callbackYes: @escaping (UIAlertAction)->(), callbackNo: @escaping (UIAlertAction)->(), buttonText: [String]) {
// create the alert
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: buttonText[0], style: UIAlertActionStyle.default, handler: callbackYes))
alert.addAction(UIAlertAction(title: buttonText[1], style: UIAlertActionStyle.cancel, handler: callbackNo))
// show the alert
alert.view.setNeedsLayout()
reference.present(alert, animated: true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment