iOS Developer Library

Developer

UIKit Framework Reference UIAlertView Class Reference

Options
Deployment Target:

On This Page
Language:

UIAlertView

Inherits From


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 2.0 and later

Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.

In apps that run in versions of iOS prior to iOS 8, use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet).

Use the properties and methods defined in this class to set the title, message, and delegate of an alert view and configure the buttons. You must set a delegate if you add custom buttons. The delegate should conform to the UIAlertViewDelegate protocol. Use the show method to display an alert view once it is configured.

Subclassing Notes

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

For more information about appearance and behavior configuration, see Alert Views.

  • Convenience method for initializing an alert view.

    Declaration

    Swift

    init(title title: String?, message message: String?, delegate delegate: AnyObject?, cancelButtonTitle cancelButtonTitle: String?)

    Objective-C

    - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles,, ...

    Parameters

    title

    The string that appears in the receiver’s title bar.

    message

    Descriptive text that provides more details than the title.

    delegate

    The receiver’s delegate or nil if it doesn’t have a delegate.

    cancelButtonTitle

    The title of the cancel button or nil if there is no cancel button.

    Using this argument is equivalent to setting the cancel button index to the value returned by invoking addButtonWithTitle: specifying this title.

    otherButtonTitles,

    The title of another button.

    Using this argument is equivalent to invoking addButtonWithTitle: with this title to add more buttons.

    Too many buttons can cause the alert view to scroll. For guidelines on the best ways to use an alert in an app, see Temporary Views.

    ...

    Titles of additional buttons to add to the receiver, terminated with nil.

    Return Value

    Newly initialized alert view.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

    Not available in app extensions

  • delegate delegate Property

    The receiver’s delegate or nil if it doesn’t have a delegate.

    Declaration

    Swift

    unowned(unsafe) var delegate: AnyObject?

    Objective-C

    @property(nonatomic, assign) id delegate

    Discussion

    See UIAlertViewDelegate Protocol Reference for the methods this delegate should implement.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • The kind of alert displayed to the user.

    Declaration

    Swift

    var alertViewStyle: UIAlertViewStyle

    Objective-C

    @property(nonatomic, assign) UIAlertViewStyle alertViewStyle

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 5.0 and later

  • title title Property

    The string that appears in the receiver’s title bar.

    Declaration

    Swift

    var title: String

    Objective-C

    @property(nonatomic, copy) NSString *title

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • message message Property

    Descriptive text that provides more details than the title.

    Declaration

    Swift

    var message: String?

    Objective-C

    @property(nonatomic, copy) NSString *message

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • visible visible Property

    A Boolean value that indicates whether the receiver is displayed. (read-only)

    Declaration

    Swift

    var visible: Bool { get }

    Objective-C

    @property(nonatomic, readonly, getter=isVisible) BOOL visible

    Discussion

    If YEStrue, the receiver is displayed; otherwise, NOfalse.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • Adds a button to the receiver with the given title.

    Declaration

    Swift

    func addButtonWithTitle(_ title: String) -> Int

    Objective-C

    - (NSInteger)addButtonWithTitle:(NSString *)title

    Parameters

    title

    The title of the new button.

    Return Value

    The index of the new button. Button indices start at 0 and increase in the order they are added.

    Discussion

    Adding too many buttons can cause the alert view to scroll. For guidelines on the best ways to use an alert in an app, see Temporary Views.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

    See Also

    message

  • The number of buttons on the alert view. (read-only)

    Declaration

    Swift

    var numberOfButtons: Int { get }

    Objective-C

    @property(nonatomic, readonly) NSInteger numberOfButtons

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • Returns the title of the button at the given index.

    Declaration

    Swift

    func buttonTitleAtIndex(_ buttonIndex: Int) -> String!

    Objective-C

    - (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex

    Parameters

    buttonIndex

    The index of the button. The button indices start at 0.

    Return Value

    The title of the button specified by index buttonIndex.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

    See Also

    “Displaying”

  • Returns the text field at the given index

    Declaration

    Swift

    func textFieldAtIndex(_ textFieldIndex: Int) -> UITextField?

    Objective-C

    - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex

    Parameters

    textFieldIndex

    The index of the text field. The text field indices start at 0.

    Return Value

    The text field specified by index textFieldIndex.

    Discussion

    The number of text fields present in an alert is dependent on the style of the alert.

    Alert Style

    Text Fields

    UIAlertViewStyleDefault

    No user-editable text fields.

    UIAlertViewStyleSecureTextInput

    A single text field at index 0.

    UIAlertViewStylePlainTextInput

    A single text field at index 0.

    UIAlertViewStyleLoginAndPasswordInput

    The login field is at index 0. The password field is at index 1.

    If your application attempts to retrieve a text field with an index that is out of bounds, the alert raises an NSRangeException.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 5.0 and later

  • The index number of the cancel button.

    Declaration

    Swift

    var cancelButtonIndex: Int

    Objective-C

    @property(nonatomic) NSInteger cancelButtonIndex

    Discussion

    The button indices start at 0. If -1, then the index is not set.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • The index of the first other button. (read-only)

    Declaration

    Swift

    var firstOtherButtonIndex: Int { get }

    Objective-C

    @property(nonatomic, readonly) NSInteger firstOtherButtonIndex

    Discussion

    The button indices start at 0. If -1, then the index is not set. This property is ignored if there are no other buttons. The default value is -1.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • Displays the receiver using animation.

    Declaration

    Swift

    func show()

    Objective-C

    - (void)show

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • Dismisses the receiver, optionally with animation.

    Declaration

    Swift

    func dismissWithClickedButtonIndex(_ buttonIndex: Int, animated animated: Bool)

    Objective-C

    - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated

    Parameters

    buttonIndex

    The index of the button that was clicked just before invoking this method. The button indices start at 0.

    animated

    YEStrue if the receiver should be removed by animating it first; otherwise, NOfalse if it should be removed immediately with no animation.

    Discussion

    In iOS 4.0, you may want to call this method whenever your application moves to the background. An alert view is not dismissed automatically when an application moves to the background. This behavior differs from previous versions of the operating system, where they were canceled automatically when the application was terminated. Dismissing the alert view gives your application a chance to save changes or abort the operation and perform any necessary cleanup in case your application is terminated later.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • The presentation style of the alert.

    Declaration

    Swift

    enum UIAlertViewStyle : Int { case Default case SecureTextInput case PlainTextInput case LoginAndPasswordInput }

    Objective-C

    typedef enum { UIAlertViewStyleDefault = 0, UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, UIAlertViewStyleLoginAndPasswordInput } UIAlertViewStyle;

    Constants

    • Default

      UIAlertViewStyleDefault

      A standard alert.

      Available in iOS 5.0 and later

    • SecureTextInput

      UIAlertViewStyleSecureTextInput

      An alert that allows the user to enter text. The text field is obscured.

      Available in iOS 5.0 and later

    • PlainTextInput

      UIAlertViewStylePlainTextInput

      An alert that allows the user to enter text.

      Available in iOS 5.0 and later

    • LoginAndPasswordInput

      UIAlertViewStyleLoginAndPasswordInput

      An alert that allows the user to enter a login identifier and password.

      Available in iOS 5.0 and later

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 5.0 and later