iOS Developer Library — Pre-Release

Developer

UIKit Framework Reference UIAlertController Class Reference

Options
Deployment Target:

On This Page
Language:

UIAlertController

A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method. More...

Inheritance


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 8.0 and later.
  • Creates and returns a view controller for displaying an alert to the user.

    Declaration

    Swift

    convenience init(title title: String?, message message: String?, preferredStyle preferredStyle: UIAlertControllerStyle)

    Objective-C

    + (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle

    Parameters

    title

    The title of the alert. Use this string to get the user’s attention and communicate the reason for the alert.

    message

    Descriptive text that provides additional details about the reason for the alert.

    preferredStyle

    The style to use when presenting the alert controller. Use this parameter to configure the alert controller as an action sheet or as a modal alert.

    Return Value

    An initialized alert controller object.

    Discussion

    After creating the alert controller, configure any actions that you want the user to be able to perform by calling the addAction: method one or more times. When specifying a preferred style of UIAlertControllerStyleAlert, you may also configure one or more text fields to display in addition to the actions.

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • title title Property

    The title of the alert.

    Declaration

    Swift

    var title: String?

    Objective-C

    @property(nonatomic, copy) NSString *title

    Discussion

    The title string is displayed prominently in the alert or action sheet. You should use this string to get the user’s attention and communicate the reason for displaying the alert.

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • message message Property

    Descriptive text that provides more details about the reason for the alert.

    Declaration

    Swift

    var message: String?

    Objective-C

    @property(nonatomic, copy) NSString *message

    Discussion

    The message string is displayed below the title string and is less prominent. Use this string to provide additional context about the reason for the alert or about the actions that the user might take.

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • The style of the alert controller. (read-only)

    Declaration

    Swift

    var preferredStyle: UIAlertControllerStyle { get }

    Objective-C

    @property(nonatomic, readonly) UIAlertControllerStyle preferredStyle

    Discussion

    The value of this property is set to the value you specified in the alertControllerWithTitle:message:preferredStyle: method. This value determines how the alert is displayed onscreen.

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • Attaches an action object to the alert or action sheet.

    Declaration

    Swift

    func addAction(_ action: UIAlertAction)

    Objective-C

    - (void)addAction:(UIAlertAction *)action

    Parameters

    action

    The action object to display as part of the alert. Actions are displayed as buttons in the alert. The action object provides the button text and the action to be performed when that button is tapped.

    Discussion

    If your alert has multiple actions, the order in which you add those actions determines their order in the resulting alert or action sheet.

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • actions actions Property

    The actions that the user can take in response to the alert or action sheet. (read-only)

    Declaration

    Swift

    var actions: [AnyObject] { get }

    Objective-C

    @property(nonatomic, readonly) NSArray *actions

    Discussion

    The actions are in the order in which you added them to the alert controller. This order also corresponds to the order in which they are displayed in the alert or action sheet. The second action in the array is displayed below the first, the third is displayed below the second, and so on.

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • Adds a text field to an alert.

    Declaration

    Swift

    func addTextFieldWithConfigurationHandler(_ configurationHandler: ((UITextField!) -> Void)!)

    Objective-C

    - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler

    Parameters

    configurationHandler

    A block for configuring the text field prior to displaying the alert. This block has no return value and takes a single parameter corresponding to the text field object. Use that parameter to change the text field properties.

    Discussion

    Calling this method adds an editable text field to the alert. You can call this method more than once to add additional text fields. The text fields are stacked in the resulting alert.

    You can add a text field only if the preferredStyle property is set to UIAlertControllerStyleAlert.

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • The array of text fields displayed by the alert. (read-only)

    Declaration

    Swift

    var textFields: [AnyObject]? { get }

    Objective-C

    @property(nonatomic, readonly) NSArray *textFields

    Discussion

    Use this property to access the text fields displayed by the alert. The text fields are in the order in which you added them to the alert controller. This order also corresponds to the order in which they are displayed in the alert.

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • Constants indicating the type of alert to display.

    Declaration

    Swift

    enum UIAlertControllerStyle : Int { case ActionSheet case Alert }

    Objective-C

    typedef enum UIAlertControllerStyle: NSInteger { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } UIAlertControllerStyle;

    Constants

    • ActionSheet

      UIAlertControllerStyleActionSheet

      An action sheet displayed in the context of the view controller that presented it.

      Use an action sheet to present the user with a set of alternatives for how to proceed with a given task. You can also use this style to prompt the user to confirm a potentially dangerous action.

      Available in iOS 8.0 and later.

    • Alert

      UIAlertControllerStyleAlert

      An alert displayed modally for the app.

      Available in iOS 8.0 and later.

    Import Statement

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.