UIAlertViewDelegate Protocol Reference

Conforms to
Framework
/System/Library/Framework/UIKit.framework
Availability
Available in iOS 2.0 and later.
Declared in
UIAlertView.h
Related sample code

Overview

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.”

Tasks

Responding to Actions

Customizing Behavior

Canceling

Instance Methods

alertView:clickedButtonAtIndex:

Sent to the delegate when the user clicks a button on an alert view.

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIAlertView.h

alertView:didDismissWithButtonIndex:

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

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIAlertView.h

alertView:willDismissWithButtonIndex:

Sent to the delegate before an alert view is dismissed.

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIAlertView.h

alertViewCancel:

Sent to the delegate before an alert view is canceled.

- (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.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIAlertView.h

alertViewShouldEnableFirstOtherButton:

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

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
Parameters
alertView

The alert view that is being configured.

Return Value

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

Availability
  • Available in iOS 5.0 and later.
Declared In
UIAlertView.h

didPresentAlertView:

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

- (void)didPresentAlertView:(UIAlertView *)alertView
Parameters
alertView

The alert view that was displayed.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIAlertView.h

willPresentAlertView:

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

- (void)willPresentAlertView:(UIAlertView *)alertView
Parameters
alertView

The alert view that is about to be displayed.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIAlertView.h