iPhone app with tab bar and navigation bar as peers

Posted by Mac on Stack Overflow See other posts from Stack Overflow or by Mac
Published on 2010-04-15T06:08:55Z Indexed on 2010/04/15 6:13 UTC
Read the original article Hit count: 481

I'm trying to write an application that uses a navigation bar and tab bar in what (I'm gathering) is an unusual manner.

Basically, I've got several "pages" ("home", "settings", etc) that each have their own tab. I'd also like to have it so that the "home" page is the root view of the navigation bar, and the other pages are the second-level views of the navigation bar. That is, I should be able to navigate to any page by clicking the appropriate tab bar item, and should be able to navigate to the home page from any other page by clicking the navigation bar's back button.

Currently, I have a UINavigationBar (through a UINavigationController) and a UITabBar (through a UITabController) as children of a UIView. The various pages' view controllers are set as the tab controller's viewControllers property, and the home page's controller is also set as the navigation controller's root view. Each page view's tag is set to its index in the tab control. I have the following logic in the tab controller's didSelectViewController delegate method:

- (void) tabBarController:(UITabBarController*) tabBarController
    didSelectViewController:(UIViewController*) viewController
{
    if ([navController.viewControllers count] > 1)
        [navController popViewControllerAnimated:NO];

    [navController pushViewController:viewController animated:YES];
}

Also, in the navigation controller's didShowViewController delegate method, I have the following code:

- (void) navigationController:(UINavigationController *) navigationController
    didShowViewController:(UIViewController *)viewController
    animated:(BOOL)animated
{
    tabController.selectedIndex = viewController.view.tag;
}

The problem that's occurring is that when I run this, the navigation bar, tab bar and home page all display ok, but the tab bar will not respond to input - I cannot select a different tab.

I gather it's more usual to have the tab bar as the child of the navigation control, or vice versa. This doesn't seem to fit my approach, because I don't want to have to individually create the subordinate control each time a change occurs in the parent control - eg: recreate tab bar each time the navigation bar changes.

Does anyone have suggestions as to what's wrong and how to fix it? I'm probably missing something obvious, but whatever it is I can't seem to find it. Thanks!

EDIT: I'm guessing it has something to do with both controller's trying to have ownership of the page controller, but I can't for the life of me figure out a way around it.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uinavigationcontroller