UIAlertController was introduced in iOS 8, it’s a common user interface on most of the app in iOS. UIAlertController replaces UIAlertView and UIActionSheet.
UIAlertController has better adaptibility on iPhone and iPad user interface. Without further a do, lets see what UIAlertController can do.
Some common alert and dialogue in iOS user interface.
let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// do something
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action) in
// do something
}
alertController.addAction(OKAction)
present(alertController, animated: true)
let alertController = UIAlertController(title: nil, message: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// when cancel is tapped
}
alertController.addAction(cancelAction)
let destroyAction = UIAlertAction(title: "Destroy", style: .destructive) { (action) in
// when destroy is tapped
}
alertController.addAction(destroyAction)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action) in
// when ok is tapped
}
alertController.addAction(OKAction)
present(alertController, animated: true)
let alertController = UIAlertController(title: "Please Enter Credential", message: nil, preferredStyle: .alert)
// ADD ACTIONS HANDLER
let loginAction = UIAlertAction(title: "Login", style: .default) { (_) in
let loginTextField = alertController.textFields![0] as UITextField
let passwordTextField = alertController.textFields![1] as UITextField
// do something with after login
}
loginAction.isEnabled = false
alertController.addAction(loginAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in
// do something
}
alertController.addAction(cancelAction)
// ADD TEXT FIELDS
alertController.addTextField { (textField) in
textField.placeholder = "Email"
}
alertController.addTextField { (textField) in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
// enable login button when password is entered
NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextFieldTextDidChange, object: textField, queue: OperationQueue.main) { (notification) in
loginAction.isEnabled = textField.text != ""
}
}
// PRESENT
present(alertController, animated: true)
Sometime you want a code that can be more reusable yet simple to call from any view controller. UIAlertController always need a view controller to present it, we also can use the app’s window to present the alert. We will make it view controller independant with this code.
Fore example you can create a model class named Functions.swift
.
class func showAlert(title: String, message: String){
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(OKAction)
var alertWindow : UIWindow!
alertWindow = UIWindow.init(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController.init()
alertWindow.windowLevel = UIWindowLevelAlert + 1
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alertController, animated: true)
}
In any view controller you may call
Functions.showAlert(title: "Title", message: "Message")