Recipe: Using Blocks with Alerts

Using an alert’s delegate callbacks can produce unnecessarily complex code. All your code handling ends up in a common routine. You must implement tagging to differentiate which alert your method must handle. You must vigilantly track which button corresponds to the functionality you wish to execute, such as the alert in Figure 3-2.

Image

Figure 3-2 This alert processes responses in blocks passed at UIAlert button creation instead of in traditional delegate callbacks.

A much simpler solution is to assign the intended implementation when declaring the buttons themselves. Blocks were built just for this task.

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.