iOS Developer Library

Developer

UIKit Framework Reference UIAlertViewDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

UIAlertViewDelegate

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

The UIAlertViewDelegate protocol defines the methods a delegate of a UIAlertView object should implement. The delegate implements the button actions and any other custom behavior. Some of the methods defined in this protocol are optional.

If you add your own buttons or customize the behavior of an alert view, implement a delegate conforming to this protocol to handle the corresponding delegate messages. Use the delegate property of an alert view to specify one of your application objects as the delegate.

If you add your own buttons to an alert view, the delegate must implement the alertView:clickedButtonAtIndex: message to respond when those buttons are clicked; otherwise, your custom buttons do nothing. The alert view is automatically dismissed after the alertView:clickedButtonAtIndex: delegate method is invoked.

Optionally, you can implement the alertViewCancel: method to take the appropriate action when the system cancels your alert view. If the delegate does not implement this method, the default behavior is to simulate the user clicking the cancel button and closing the view.

You can also optionally augment the behavior of presenting and dismissing alert views using the methods in Customizing Behavior.

Inheritance


Not Applicable

Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 2.0 and later.
  • Sent to the delegate when the user clicks a button on an alert view.

    Declaration

    Swift

    optional func alertView(_ alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)

    Objective-C

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

    Parameters

    alertView

    The alert view containing the button.

    buttonIndex

    The index of the button that was clicked. The button indices start at 0.

    Discussion

    The receiver is automatically dismissed after this method is invoked.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Sent to the delegate to determine whether the first non-cancel button in the alert should be enabled.

    Declaration

    Swift

    optional func alertViewShouldEnableFirstOtherButton(_ alertView: UIAlertView) -> Bool

    Objective-C

    - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView

    Parameters

    alertView

    The alert view that is being configured.

    Return Value

    YEStrue if the button should be enabled, no if the button should be disabled.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • Sent to the delegate before a model view is presented to the user.

    Declaration

    Swift

    optional func willPresentAlertView(_ alertView: UIAlertView)

    Objective-C

    - (void)willPresentAlertView:(UIAlertView *)alertView

    Parameters

    alertView

    The alert view that is about to be displayed.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Sent to the delegate after an alert view is presented to the user.

    Declaration

    Swift

    optional func didPresentAlertView(_ alertView: UIAlertView)

    Objective-C

    - (void)didPresentAlertView:(UIAlertView *)alertView

    Parameters

    alertView

    The alert view that was displayed.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Sent to the delegate before an alert view is dismissed.

    Declaration

    Swift

    optional func alertView(_ alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int)

    Objective-C

    - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

    Parameters

    alertView

    The alert view that is about to be dismissed.

    buttonIndex

    The index of the button that was clicked. The button indices start at 0. If this is the cancel button index, the alert view is canceling. If -1, the cancel button index is not set.

    Discussion

    This method is invoked before the animation begins and the view is hidden.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Sent to the delegate after an alert view is dismissed from the screen.

    Declaration

    Swift

    optional func alertView(_ alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int)

    Objective-C

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

    Parameters

    alertView

    The alert view that was dismissed.

    buttonIndex

    The index of the button that was clicked. The button indices start at 0. If this is the cancel button index, the alert view is canceling. If -1, the cancel button index is not set.

    Discussion

    This method is invoked after the animation ends and the view is hidden.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Sent to the delegate before an alert view is canceled.

    Declaration

    Swift

    optional func alertViewCancel(_ alertView: UIAlertView)

    Objective-C

    - (void)alertViewCancel:(UIAlertView *)alertView

    Parameters

    alertView

    The alert view that will be canceled.

    Discussion

    If the alert view’s delegate does not implement this method, clicking the cancel button is simulated and the alert view is dismissed. Implement this method if you need to perform some actions before an alert view is canceled. An alert view can be canceled at any time by the system—for example, when the user taps the Home button. The alertView:willDismissWithButtonIndex: and alertView:didDismissWithButtonIndex: methods are invoked after this method.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.