UIAlertView: how to substitute the old UIAlertView.

We see how to substitute the old UIAlertView, deprecated in iOS9.
There was changes in iOS8, to make the interfaces more adaptive.
Therefore in the new adaptive world of iOS 8 the UIAlertController is a replacement for both UIAlertView and UIActionSheet. Switching between an alert or action sheet is done by setting the preferred style when creating the controller.

For example we consider a message with the OK button. The old code was like this:

func showSendMailErrorAlert() {
// Old and deprecated

let sendMailErrorAlert = UIAlertView(title: “some title”, message: “some message”, delegate: self, cancelButtonTitle: “OK”)


After that I substituted the code with this new code:
func showSendMailErrorAlert() {
let alertController = UIAlertController(title: “some title”, message: “Some message.”, preferredStyle: .alert)
let OKAction = UIAlertAction(title: “OK”, style: .default)
var alertWindow : UIWindow!
alertWindow = UIWindow.init(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController.init()
alertWindow.windowLevel = UIWindow.Level.alert + 1
alertWindow.rootViewController?.present(alertController, animated: true)

Leave a Reply