views:

693

answers:

1

This question has probably been asked before, but my google-fu must be inferior to everybody else's, cause I can't figure this out.

I'm playing around with the iPhone SDK, and I'm building a concept app I've been thinking about. If we have a look at the skeleton generated with a navigation based app, the MainWindow.xib contains a navigation controller, and within that a root-view controller (and a navigation bar and toolbar if you play around with it a little). The root-view controller has the RootViewController-nib associated with it, which loads the table-view.

So far so good. To add content to the tool bar and to the navigation bar, I'm supposed to add those to in the hierarchy below the Root View Controller (which works, no problem). However, what I can't figure out is, this is all still within the MainWindow.xib (or, at runtime, nib). How would I define a xib in order for it to pick up tool bar items from that?

I want to do (the equivalent of, just reusing the name here)

RootViewController *controller = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

and have the navigation controller pick-up on the tool bar items defined in that nib. The logical place to put it would be in the hierarchy under File's Owner (which is of type RootViewController), but it doesn't appear to be possible.

Currently, I'm assigning these (navigationItem and toolbarItems) manually in the viewDidLoad method, or define them in the MainWindow.xib directly to be loaded when the app initializes.

Any ideas?

Edit

I guess I'll try to explain with a picture. This is the Interface Builder of the main window, pretty much as it comes out of the wizard to create a navigation based project. I've added a toolbar item for clarity though. You can see the navigation controller, with a toolbar and a navigation bar, and the root view controller.

IB Screenshot

Basically, the Root View Controller has a bar button item and a navigation item as you can see. The thing is, it's also got a nib associated with it, which, when loaded will instantiate a view, and assign it to the view outlet of the controller (which in that nib is File's Owner, of type RootViewController, as should be).

How can I get the toolbar item, and the navigation item, into the other nib, the RootViewController.nib so I can remove them here. The RootViewController.nib adds everything else to the Root View Controller, why not these items?

The background for this is that I want to simply instantiate RootViewController, initialize it with its own nib (i.e. initWithNibName:nil shown above), and push it onto the navigation controller, without having to add the navigation/toolbar items in coding (as I do it now).

A: 

First off, the way you've worded your question is a bit confusing here, but I think I understand.

You've got a couple options here. If you want to design your toolbar, just add one to the view you are loading and drag the various buttons and separators onto it in Interface Builder. Then create IBActions in that view's view controller and drag connections from your toolbar items to the various actions. Since you are using a Navigation controller, though, you will want to hide the toolbar you get by default by unchecking the "Shows Toolbar" box in your MainWindow.xib when the UINavigationController is selected (Attributes tab in the IB inspector).

alt text

Alternatively, you can just add toolbar items programmatically in the viewDidLoad method by calling [self setToolbarItems:items]; where items is an NSArray of toolbar items.

Really there is no way that I'm aware of to cause the navigation controller to use your custom toolbar. Just hide the main one and use your custom one.

Matt Long
Hi Matt, I guess my question is too confusing.. :) I'm not sure how to make it clearer though. What I'm trying to do is this: it appears in the IB as if you must assign toolbar items to a uicontroller (makes sense..), however, if I'd like this uicontroller to be defined in a separate nib from where the navigation controller is defined (a nib only for my root view controller), I guess I'd have to assign them to File's Owner (since it's already been created), but the IB won't let me. How would I go about defining the toolbar items, if not programmatically which I do now? Does that clear it up?:)
roe
I mean, the ui-controller shouldn't be in a separate nib, it'll be allocated programmatically, I'd just like to initialize it with a nib containing the toolbar items. Maybe that's a better way to put it.
roe
You can get an array of controls from any nib by calling: NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"ToolbarViewController" owner:self options:nil]; Then you can access each view by array index or tag (which you can specify in IB). Not sure I'm understanding the issue still, though.
Matt Long
Ok, I'll try to get some screen shots done tonight, maybe I can express it better in pictures.
roe
I've tried to explain it a bit more clear, did it work? :)
roe