Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
Like this article? We recommend

Welcome, UIAlertController

In place of the venerable UIAlertView class, iOS 8 supplies the UIAlertController class. The main aim of this change is to ensure that UI elements remain adaptive when viewed on different types of iOS devices.

Let's take a look at a simple example and expand on it. The following code snippet displays an alert window:

var alertController = UIAlertController(
  title: "Hello, World!",
  message: "Nice to meet you!",
  preferredStyle: UIAlertControllerStyle.Alert)

var okAction = UIAlertAction(
  title: "OK", style: UIAlertActionStyle.Default) {
      (action) -> Void in
       println("Tapped on OK")
}

alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion:nil)

Figure 3 shows the alert.

Figure 3

Figure 3 The new UIAlertController class in action.

Notice the following changes:

  • You use the UIAlertController class to create an alert.
  • You can choose between two styles—Alert or ActionSheet (more on this style later in the article).
  • To display buttons in the alert, you need to create separate instances of UIAlertAction class and then add it to the UIAlertController instance explicitly.
  • The UIAlertAction class supports three styles—Default, Destructive, and Cancel.
  • You no longer have to implement separate methods to handle an event that's fired when the user taps a button. The result is handled directly within the initializer of the UIAlertAction class.
  • To display an alert, you need to present the alert modally by using the presentViewController method of the UIViewController class.

To add another button, simply create another instance of the UIAlertAction class and add it to the UIAlertController instance:

var alertController = UIAlertController(
  title: "Hello, World!",
  message: "Nice to meet you!",
  preferredStyle: UIAlertControllerStyle.Alert)

var okAction = UIAlertAction(
  title: "OK", style: UIAlertActionStyle.Default) {
      (action) -> Void in
       println("Tapped on OK")
}

var cancelAction = UIAlertAction(
  title: "Cancel", style: UIAlertActionStyle.Default) {
      (action) -> Void in
       println("Tapped on Cancel")
}

alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)

As Figure 4 shows, the alert now has two buttons.

Figure 4

Figure 4 Displaying two buttons in the alert.

If you want to draw the user's attention to a button (such as confirming a delete operation), you can set the style of the UIAlertAction to Destructive:

var okAction = UIAlertAction(
  title: "OK", style: UIAlertActionStyle.Destructive){
      (action) -> Void in
       println("Tapped on OK")
}

The OK button is now displayed in red (see Figure 5):

Figure 5

Figure 5 A destructive button is displayed in red.

  • + Share This
  • 🔖 Save To Your Account