views:

30

answers:

2

My tab bar has ten tabs. Six of these are therefore shoved into the "More" tab. Several of them do not have UINavigationControllers. I.e, the tab is controlled by a UIViewController subclass that is not a navigation controller.

When the user selects one of these, the appropriate view controller come sup, with a UINavigationBar at the top.

I want to add a button to that navigation bar. How can I do that?

Thanks.

A: 

There are two ways to go about it.

  1. Just use a UINavigationController for each tab, with your view controller in it. But it sounds like you don't want to do this, so:

  2. Put a UINavigationBar in using Interface Builder. nav bar
    You can then drag in UIBarButtonItems and configure them to your liking, also in IB. buttons

jtbandes
A: 

Well, if you want to do this programmatically, then I would suggest replacing each of your viewControllers in your UITabBar with UINavigationControllers that house the respective view controllers.

So, your old code looks something like this:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  UITabBarController *tbc = [[[UITabBarController alloc]init]autorelease];
  [window addSubview:tbc.view];
  UIViewController *mapVC = [[[UIViewController alloc] init]autorelease];
  NSArray *tabViewControllerArray = [NSArray arrayWithObjects:self.mapVC, nil];
  tbc.viewControllers = tabViewControllerArray;
}

New code should be:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  UITabBarController *tbc = [[[UITabBarController alloc]init]autorelease];
  [window addSubview:tbc.view];
  UIViewController *mapVC = [[[UIViewController alloc] init]autorelease];
  // add the viewController to a UINavigationController
  UINavigationController *mapNav = [[[UINavigationController alloc] initWithRootViewController:mapVC]autorelease];
  // put the nav controller in the array instead
  NSArray *tabViewControllerArray = [NSArray arrayWithObjects:mapNav, nil];
  tbc.viewControllers = tabViewControllerArray;

  // this code adds a right button to the mapBav navigationBar
  // this uses a custom view, but you could use a standard UIBarButtonItem too 
  NSArray *items = [NSArray arrayWithObjects: [UIImage imageNamed:@"flag-icon.png"], nil];
  UISegmentedControl *tableControl = [[[UISegmentedControl alloc] initWithItems:items]autorelease];
  tableControl.segmentedControlStyle = UISegmentedControlStyleBar;
  UIBarButtonItem *segmentBarItem = [[[UIBarButtonItem alloc] initWithCustomView:tableControl] autorelease];
  self.navigationItem.rightBarButtonItem = segmentBarItem;
}
Andrew Johnson