Macros

Macros simplify your work when building bar buttons because the creation task is so repetitive. The following macro creates a basic button item:

#define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] \     initWithTitle:TITLE style:UIBarButtonItemStylePlain \     target:self action:SELECTOR]

You supply it with a title and a selector to call. Each call to this macro specifies only the title and selector, tightening up the code’s readability:

self.navigationItem.rightBarButtonItem =     BARBUTTON(@"Push", @selector(push:));

This version of the macro assumes that the target is "self", which is quite common, although you could easily adapt this. The following macro adds a target ...

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.