Setting Toolbar Items of UINavigationController
Asked Answered
P

2

7

In iPhone OS 3.0, you can set the toolbar items of a UINavigationController using the setToolbarItems:animated: method. However, this requires you pass in an array of UIToolbarItems. While I could programmatically create these toolbar items, I'd rather create them in Interface Builder if possible.

With this in mind, I have created a UIToolbar in "MyGreatViewController.xib" and have populated it with the wanted toolbar items. Then, in "MyGreatViewController.m", I get the items from the toolbar and pass them to setToolbarItems:animated::

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setToolbarItems: [toolbar items]];
}

...where toolbar is an IBOutlet referring to the UIToolbar.

Is this a good approach? Is there a better way to accomplish this? Should I just create the items programmatically?

Petit answered 22/8, 2009 at 9:16 Comment(1)
I don't understand something: You say "I get the items from the toolbar", but then you give them back to the toolbar? Aren't they already displayed?Spreadeagle
M
4

It's a perfectly acceptable way of doing it, but do bear in mind that loading xib files is quite expensive on the iPhone, and it may well be faster to create the toolbar items programatically in your viewDidLoad method.

Maxwell answered 22/8, 2009 at 9:20 Comment(2)
Great. Since the XIB is already being loaded when the view controller class initialises (I checked "With XIB for user interface" when creating the view controller files), this approach shouldn't be any more expensive than programmatically creating the toolbar items.Petit
Ah, if you're already loading an XIB then you won't add too much extra time. Unarchiving objects from an XIB is more expensive than creating them in code, but unless your view takes ages to load I wouldn't worry about it.Maxwell
C
12

I don't know if this is documented anywhere, but I've found that in Interface Builder, if you enable the navigation controller's toolbar, you can drag bar items to your view controller, and they will automagically show up in the navigation controller's toolbar.

For example, here's what we can do (using Xcode 3.2 on Snow Leopard):

  1. File->New Project.... Choose Navigation-based Application and create the project.
  2. Open MainWindow.xib in Interface Builder.
  3. Select the Navigation Controller, and in the Attributes inspector, check the "Shows Toolbar" box. This will cause a Toolbar object to appear.
  4. Drag a Bar Button Item from the Library to the toolbar. It will appear in the toolbar. If you check the hierarchy in the NIB, you'll see that this new item is a child of the RootViewController.

It seems that any Bar Button Items added as children of the navigation item will show up in the navigation bar, and any Bar Button Items added as children of the view controller will show up in the toolbar.

(I stumbled on this by accident. If anyone can find documentation for this behavior, or any additional info, I'd like to hear about it.)

Camilacamile answered 20/9, 2009 at 19:43 Comment(5)
I believe that's just the way it's supposed to be. Kind of like UIViewController magically loading it's xib if it has a matching filename.Sutter
It works! This is definitely the best way to set up the toolbar in a navigation view, I think.Munson
In my application, I have a Tab Bar Controller and one of the tabs is a navigationController - pretty standard configuration. However if I follow what Kristopher is suggesting, after step 3, the toolbar appears behind the Tab Bar in the IB! And when I try step 4 (I tried to add segment control/button), it just does not allow my to drop it on toolbar. Am I missing something here?Watters
I know this is super old, but do you have any idea how to do this for subviews of the RootViewController? In other words, if in RootViewController's code I create a FooController that is in its own nib and push it, then how can I set FooController's toolbar buttons in Interface Builder?Merriott
@Merriott agree, I'd also like to know if there is a solution to do this with IBFritzfritze
M
4

It's a perfectly acceptable way of doing it, but do bear in mind that loading xib files is quite expensive on the iPhone, and it may well be faster to create the toolbar items programatically in your viewDidLoad method.

Maxwell answered 22/8, 2009 at 9:20 Comment(2)
Great. Since the XIB is already being loaded when the view controller class initialises (I checked "With XIB for user interface" when creating the view controller files), this approach shouldn't be any more expensive than programmatically creating the toolbar items.Petit
Ah, if you're already loading an XIB then you won't add too much extra time. Unarchiving objects from an XIB is more expensive than creating them in code, but unless your view takes ages to load I wouldn't worry about it.Maxwell

© 2022 - 2024 — McMap. All rights reserved.