UIAlertController Examples

3 minutes, 33 seconds

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.

UIAlertController Examples

Some common alert and dialogue in iOS user interface.

  1. Basic Alert
  2. Action Sheet
  3. Alert Login Form
  4. Info Alert (Use as Class Method)

1. Basic Alert

basic.png


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)

2. Action Sheet

A


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) 

3. Alert Login Form

login.png


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)

4. Info Alert (Use as Class Method)

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.

AAA

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")
  • Roger

    I just read your blog on UIAlertController Examples….and it was great. I had a major problem with doing alerts from non-UIController code and some delegate functions (like imagePicker) that were even in a controllers. Kept getting ‘no view’ type of error messages. I searched a lot, and after trying lots of other solutions, I finally found the solution in #4. Info Alert….and it works great. Nothing else I tried worked…even though people said it worked. So….thanks a lot for writing this up. You saved me a huge rewrite. Have a great day!!!…….Roger

    • Hijazi

      Thanks Roger! Happy coding : )