Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add multiple UIBarButtonItem's to a UINavigationBar?

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?

like image 388
Sheehan Alam Avatar asked Mar 26 '12 05:03

Sheehan Alam


2 Answers

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.

like image 186
jonkroll Avatar answered Oct 21 '22 05:10

jonkroll


You should use

self.navigationItem.leftBarButtonItems = items;
like image 39
xda1001 Avatar answered Oct 21 '22 05:10

xda1001