Skip to content

Instantly share code, notes, and snippets.

@abarrak
Created February 19, 2017 11:05
Show Gist options
  • Select an option

  • Save abarrak/eae35721ea30aa04ac912d6dd00ffc30 to your computer and use it in GitHub Desktop.

Select an option

Save abarrak/eae35721ea30aa04ac912d6dd00ffc30 to your computer and use it in GitHub Desktop.
Alert snippet in Swift
//
// 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