7

I want to add many UIBarButtonItem's to a UINavigationbar, not just right and left buttons:

logoButton = [[UIBarButtonItem alloc] initWithTitle:@"A Button" style:UIBarButtonItemStyleBordered target:self action:@selector(logoButtonAClicked:)];

logoButton2 = [[UIBarButtonItem alloc] initWithTitle:@"B Button" style:UIBarButtonItemStyleBordered target:self action:@selector(logoButtonBClicked:)];

logoButto3 = [[UIBarButtonItem alloc] initWithTitle:@"C Button" style:UIBarButtonItemStyleBordered target:self action:@selector(logoButtonCClicked:)];

self.navigationController.navigationBarHidden = NO;

self.title = @"Title";

NSArray* items = [[NSArray alloc] initWithObjects:logoButtonA, logoButtonB, logoButtonC, nil];
self.navigationController.navigationBar.items = items;

I get a SIGBRT on self.navigationController.navigationBar.items = items;

How can I add multiple UIBarButtonItems to a UINavigationBar?

Ash Furrow
  • 12,381
  • 3
  • 56
  • 91
Sheehan Alam
  • 58,693
  • 123
  • 352
  • 554
  • possible duplicate of [Multiple UIBarButtonItems in UINavigationBar](http://stackoverflow.com/questions/5100840/multiple-uibarbuttonitems-in-uinavigationbar) – jscs Mar 26 '12 at 05:59

3 Answers3

17

You need to add UIBarButtonItem instance to a UINavigationItem, not to a UINavigationBar. So you can do this as:

NSArray *buttonArray = [NSArray arrayWithObjects:logoButton, logoButton2, logoButton3, nil];
self.navigationItem.leftBarButtonItems = buttonArray;

If you want your buttons on the right, use rightBarButtonItems.

Ash Furrow
  • 12,381
  • 3
  • 56
  • 91
jonkroll
  • 15,614
  • 4
  • 49
  • 42
4

You should use

self.navigationItem.leftBarButtonItems = items;
xda1001
  • 2,419
  • 15
  • 18
-1

use addSubView in navigationbar.

Prashant Bhayani
  • 672
  • 4
  • 18
  • Not a great idea ... those buttons don't look or behave like standard `UIBarButtonItem` instances and, additionally, `UIBarButtonItem` isn't a subclass of `UIView`, so you can't call [navigationBar addSubview:logoButton]`. – Ash Furrow Mar 27 '12 at 02:12
  • -1: From `UINavigationBar` docs: `When you use a navigation bar as a standalone object, you are responsible for providing its contents. Unlike other types of views, you do not add subviews to a navigation bar directly. Instead, you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed.` – JRG-Developer Feb 05 '13 at 20:32