views:

323

answers:

2

Another iPhone noob question.

The app I'm building needs to show a shared custom UIToolbar for multiple views (and their subviews) within a UITabBarController framework. The contents of the custom toolbar are the same across all the views. I'd like to be able to design the custom toolbar as a xib and handle UI events from its own controller class (I'm assuming I can subclass UIToolbar to do so?). That way I could define IBOutlet & IBAction items, etc. Then I could associate this custom toolbar with each of the UITabBarController views (and their subviews). But I'm having trouble finding out whether that's possible - and if so, how to do it.

In particular, I want to be able to push new views onto UINavigationController view stacks which are each associated with parent UITabBarController tabs. So, to summarize, I want a:

  • custom toolbar
  • shared by multiple views
  • which are managed by multiple navigation controllers
  • and the navigation controllers are associated with different tabs of a parent tab bar controller

The tab bar controller itself is launched modally, though I don't believe that's relevant.

Anyway, the tab bar controller is working, as are its child navigation controllers. I'm just having a little trouble figuring out how to persist the shared toolbar to the various subviews. I'd settle for a good clean way of implementing programmatically... though I'd prefer the flexibility of keeping the toolbar's visual design in a xib.

Anyone have any suggestions?

+1  A: 

A few initial thoughts.

You will probably want to keep this toolbar outside of the scope of the tabbar and just propagate events that happen from the toolbar to the view on screen.

As it stands, the toolbar is managing several views and I do not believe you can have components floating on top of a particular view inside of the controller.

I typically code my UI programatic so I can have exact control, so I can't say how easy it is to manage this from Interface Builder.

David McGraw
thanks for responding... even general feedback like this is of value to me at this point. even knowing that more experienced objective-c programmers don't immediately see the solution is helpful. i'll find it easier to live with having taken a few days to find a solution to this if i know there isn't some trivial solution out there of which i'm unaware.
codemonkey
+1  A: 

I believe I have the "answer"... and it's so simple, I'm rather embarrassed that it didn't occur to me before now. Create a custom subclass of UIViewController, within which you set the attributes of the UINavigationController's toolbar (set as enabled from within the xib in IB). Then, from whichever other views you want that toolbar included, you just subclass that class. In my case, I named a ChallengeToolbar subclass of UIViewController, then I did this:

UIImage *vaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"goldbar" ofType:@"png"]];
UISegmentedControl *vaultButtonControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:vaultImage, nil]];
vaultButtonControl.segmentedControlStyle = UISegmentedControlStylePlain;
vaultButtonControl.momentary = YES;
[vaultButtonControl addTarget:self action:@selector(goNavVault:) forControlEvents:UIControlEventAllEvents];
UIBarButtonItem *vaultButton = [[UIBarButtonItem alloc] initWithCustomView:vaultButtonControl];
vaultButton.customView.frame = CGRectMake(0,0,40,20);

UIBarButtonItem *invite = [[UIBarButtonItem alloc] initWithTitle:@"Invite" style:UIBarButtonItemStyleBordered target:self action:@selector(goNavVault:)];

UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *items = [NSArray arrayWithObjects: invite, flexItem, flexItem, flexItem, vaultButton, nil];
[self setToolbarItems:items animated:NO];
[vaultButtonControl release];
[vaultButton release];
[invite release];
[flexItem release];

... within the viewDidLoad method. Then, from any view in which I want my toolbar to appear I simply set up the view's controller class to subclass my ChallengeToolbar class instead of UIViewController. DUH!

codemonkey