4

I have a UINavigationController that I've set as the rootViewController of my window. In the NIB file I've set it up so that it has a "Bottom Bar" of "Toolbar". In Interface Builder I've added a UIBarButtonItem. This all works great and I can handle the button click fine. When I hit one of the buttons, I push a new view onto the ViewController and that works fine too. One problem, my button disappears when the view is loaded. Now in the subsequent view I can set the bottom bar to be a Toolbar and I see it in Interface Builder, but I cannot add any buttons to it.

I know I'm either missing something obvious or thinking about this incorrectly but how do I add UIBarButtonItems to subsequent views pushed to my nav controller? I'm talking the bar at the bottom, not the nav bar at the top.

Any ideas?

JamesB41
  • 703
  • 10
  • 20

2 Answers2

11

The toolbarItems property on the UIViewController is what you are interested in. You can create UIBarButtonItems programmatically and add them to a new toolBarItems array in viewDidLoad.

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem* editButton = [[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editAction)] autorelease];
    [self setToolbarItems:[NSArray arrayWithObject:editButton]];
}
  • Thank you that worked well. Is there any way to do this in Interface Builder? It seems like the vast majority of my struggles are coming from trying to design things with IB. – JamesB41 Mar 15 '11 at 03:54
  • 1
    You could create the UIBarButtonItems in interface builder, and add them in paralell to the main view (next to the view, not part of the view hierarchy). Then set them as properties on the files owner. Then, in viewDidLoad, set your toolbarItems to an array containing references to those properties. You can also try what these guys are talking about - http://stackoverflow.com/questions/1315587/setting-toolbar-items-of-uinavigationcontroller –  Mar 15 '11 at 14:08
-2

This worked better for me:

[[self navigationItem] setLeftBarButtonItem:homeButton];

You can do the same for the right side.

Steven
  • 4,826
  • 3
  • 35
  • 44