Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding items to a navigation toolbar

Tags:

iphone

I have a navigation toolbar in which I am adding toolbar items programatically, as below. The toolbar displays properly, and the toolbar style is set to black opaque. but the button on the toolbar does not display. Why?

    //Set up the toolbar
 [[[self navigationController] toolbar] setBarStyle:UIBarStyleBlackOpaque]; 
 UIBarButtonItem *myButtonItem = 
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(handleMyButton)];
 NSArray *myItems = [NSArray arrayWithObjects: myButtonItem,nil];
 [[self navigationController] setToolbarItems:myItems animated:NO];
 [myButtonItem release];
like image 425
Don Wilson Avatar asked Nov 28 '22 23:11

Don Wilson


1 Answers

UINavigationController fetches the buttons that should be used for the navigation bar and the tool bar from the current visible view controller. This means that you add the buttons you want to have to the view controller, not the navigation controller. So it should work just fine if you do:

[self setToolbarItems:myItems animated:NO];

Compare that with how the add button is added to the navigation bar in the default template for a Navigation Based Application with Core Data:

self.navigationItem.rightBarButtonItem = addButton;

This means that when you push a new view controller the buttons in the tool bar will disappear and then appear again when you pop back.

like image 133
Robert Höglund Avatar answered Dec 09 '22 15:12

Robert Höglund