Adding multiple buttons to UINavigationItem

I recently needed to add several buttons to a UINavigationItem in my iOS 6 app. All documentation I found only spoke about the standard buttons on a UINavigationItem which are backBarButtonItem, leftBarButtonItem and rightBarButtonItem. Well, not enough for me: I needed 2 buttons to be displayed on the left side of my navigation.

After hours of searching, reading and trying I found this. Maybe somebody else might need it, too:

NSMutableArray *buttons = [[NSMutableArray alloc]initWithCapacity:2];

UIBarButtonItem *button1 = [[UIBarButtonItem alloc]initWithTitle:@”button1″ style:UIBarButtonItemStylePlain target:self action:@selector(myLittleAction)];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc]initWithTitle:@”button2″ style:UIBarButtonItemStylePlain target:self action:@selector(myOtherAcion)];

[buttons addObject:button1];
[buttons addObject:button2];
[self.navigationItem setLeftBarButtonItems:buttons];

So easy and yet it took me so long to set up… 🙂

With this approach you lose the standard back button for navigation. But there’s help. In the method myLittleAction I did this:

UIViewController *prevVC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
[self.navigationController popToViewController:prevVC animated:YES];