Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

UIAlertController in iOS 8

Sayonara UIAlertView and UIActionSheet, and say hello to UIAlertController. In iOS 8 Apple introduced UIAlertController to be used in place of UIAlertView and UIActionSheet.

How do I do it?


init(title: String!, message: String!, preferredStyle: UIAlertControllerStyle)
var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)

What does the style mean?


enum UIAlertControllerStyle : Int {
    case ActionSheet
    case Alert

As you see above, I am setting the ‘preferredStyle‘ as UIAlertControllerStyle.Alert. UIAlertControllerStyle is a enum with two values, Alert and ActionSheet.

  • Alert: setting this value to Alert will display my UIAlertController in the middle of the screen, much like the UIAlertView behavior we are familiar with.
  • ActionSheet: setting this value to ActionSheet will present the UIAlertController from the bottom of the screen, much like the UIActionSheet behavior.

What about those buttons?


Great question. One used to have to adhere to the UIAlertViewDelegate protocol and implement alertView:clickedButtonAtIndex: in order to decide what should happened based on what button was clicked. Now with iOS 8 and UIAlertController you can just add an Action to the controller.

init(title: String!, style: UIAlertActionStyle, handler: ((UIAlertAction!) -> Void)!)
alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler:nil))


Checkout that handler!


Yes, since we are no longer using a Delegate to act on a button tap, you can set your resulting behavior right in your Action initialization using a Closure.

alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: {action in
            println("confirm was tapped")

In the code above, “confirm was tapped” is printed when the user taps the Confirm button.

let confirmClosure: ((UIAlertAction!) -> Void)! = { action in
            println("confirm was tapped")
alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler:confirmClosure))

In this snippet, I have created my Closure separately and then passed it in as the handler for my Action.

Using a Closure here allows for lots of creativity when deciding on how you want to programmatically react to the different actions.


What do the different types of Alert Action Styles do?


enum UIAlertActionStyle : Int {
    case Default
    case Cancel
    case Destructive

UIAlertStyle is a enum with values of Default, Cancel, and Destructive.

  • Default: Setting this value to Default will give you a button with text of a normal weight and blue color.
  • Cancel: Setting this value to Cancel will give you a button with text of a strong weight and blue color.
  • Destructive: Setting this value to Destructive will give you a button with text of a normal weight and red color.

Well that’s pretty cool


A UIAlertController will let you add multiple actions, except you can only have 1 action with a Style of Cancel. When 2 actions are on a UIAlertController, they are displayed next to each other horizontally, but when 3 or more are present they are displayed on top of each other vertically. This way, when you add a ridiculous amount of actions (like 9), your UIAlertController will become scrollable so you can see and use all of your Actions.

The Action order works just like reading, the first added action is at the top or to the left and your last Action will be at the bottom, or when there are 2 Actions, to the right.


Happy 4th & Happy Coding

  1. Dave Dunn says:

    How do I present a UIAlertController programmatically?

    I am used to using [UIActionSheet showFromBarButtonItem: animated:], or [UIActionSheet showFromView: animated:]. (Please pardon my use of Objective-C.)

  2. nishanth golla says:

    -(bool)textFieldShouldClear:(UITextField *)textField

    UIAlertController * blert = [UIAlertController alertControllerWithTitle:@”your alert” message:@”are you sure you want to clear” preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction * defautact = [UIAlertAction actionWithTitle:@”ok” style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];

    UIAlertAction * defautact1 = [UIAlertAction actionWithTitle:@”cancel” style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];

    [blert addAction:defautact];

    [blert addAction: defautact1];

    [self presentViewController:blert animated:YES completion:nil];


    Hi, Stack Overflow Community,

    I’m trying to use the “-(bool)textFieldShouldClear:(UITextField *)textField” in this
    I’m creating a alert and including two buttons “ok”and ”cancel”
    so if I press the button “ok” then the return value for the “-(bool)textFieldShouldClear:(UITextField *)textField” must be “yes”
    so if I press the button “cancel” then the return value for the “-(bool)textFieldShouldClear:(UITextField *)textField” must be “no”

    so anyone can you please tell me how to do this in the handler

    so my question is how to use the handler

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *