Simple example of displaying alerts with UIAlertController

Well, today I will introduce the UIAlertController which will almost certainly be implemented when making applications with iOS (Swift). As you all know, UIAlertView has been deprecated from iOS 8 and UIAlertController has appeared.

When you started to develop iOS, I think you often write similar code every time you display an alert (although that may not exist …)

This time I introduce a simple utility class using such UIAlertController.
Although there are various patterns even if it is called UIAlertController, here I introduce a pattern composed of common title, message content, Cancel, OK as an example.

When implementing this, the general method looks like the following.

let alert = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

present(alert, animated: true, completion: nil)

For example, it is painful to write a similar code every time you display an alert. So, I summarized the common Cancel, OK format as a utility class.

I made a simple example project called UIAlertControllerSample in the iOS_Sample repository. Usage is easy. Just call at the place you want to display the dialog as follows.

ViewController.swift

let alertTitle = "Title"
let alertMessage = "message1\nmessage2"
let positiveButtonText = "OK"
let negativeButtonText = "Cancel"
Util.sharedInstance.showAlertView(title: self.alertTitle , message: self.alertMessage, actionTitles: [self.negativeButtonText, self.positiveButtonText], actions: [
{()->() in
print(self.negativeButtonText)
},
{()->() in
print(self.positiveButtonText)
}]
)

Util.swift (for reference, this is a UIAlertController summarized as Util)

import UIKit
class Util: NSObject {
var rootWindow: UIWindow!
// Singleton.
class var sharedInstance: Util {
struct Static {
static let instance: Util = Util()
}
return Static.instance
}
private override init() {}
// show alert.
func showAlertView(
title: String? = nil,
message: String,
actionTitles: [String],
actions: [() -> ()]?) {
// create new window.
let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = UIColor.clear
window.rootViewController = UIViewController()
Util.sharedInstance.rootWindow = UIApplication.shared.windows[0]
//create alertview.
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
for title in actionTitles {
//add action.
let action = UIAlertAction(title: title, style: .default, handler: { (action : UIAlertAction) -> Void in
if let acts = actions {
if acts.count >= actionTitles.count {
acts[actionTitles.index(of: title)!]()
}
}
DispatchQueue.main.async(execute: { () -> Void in
alert.dismiss(animated: true, completion: nil)
window.isHidden = true
window.removeFromSuperview()
Util.sharedInstance.rootWindow.makeKeyAndVisible()
})
})
alert.addAction(action)
}
window.windowLevel = UIWindowLevelAlert
window.makeKeyAndVisible()
window.rootViewController?.present(alert, animated: true, completion: nil)
}
}

As a method argument, specify a title, a message body, a button corresponding to Cancel, and a button corresponding to OK with a character string. Incidentally, the argument actions is an array that allows you to set closures, so you can easily do other processing after push the OK Button.

If you want to display only the OK button, specify OK string only for actions.

I hope this helps those studying Swift and iOS App.

Show your support

Clapping shows how much you appreciated kinoko(poison)’s story.