Alert Delegates

Need to know if a user tapped OK or Cancel? Alerts use delegates to recover user choices after they’ve been made, using a simple callback. Delegates should declare the UIAlertViewDelegate protocol. In normal use, you often set the delegate to your primary (active) view controller object.

Delegate methods enable you to react as different buttons are pressed. As you’ve already seen, you can omit that delegate support if all you need to do is show some message with an OK button.

After the user has seen and interacted with your alert, the delegate receives an alertView:clickedButtonAtIndex: callback. The second parameter passed to this method indicates which button was pressed. Button numbering begins with zero. The Cancel button ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.