Search code examples
objective-cxcode4uinavigationcontrolleruitabbarcontroller

TabBarController inside NavigationController


Imagine that we have multiview apllication which is controlled by Navigation Controller. We go from the first view to second by using pushViewController method and that's not a problem but then we need to move to the third view. And the third one is a view which looks like a TabBar. How do we do that? The third view is supposed to be controlled by TabBarController, isn't it? So how to pass the control? I declared an outlet UITabBarController * tbc and connected it to TabBarController in xib file and then i tried this in viewDidLoad: tbc = [[UITabBarController alloc]init];

and it shows nothing. Your help is highly appreciated


Solution

  • It's a bit wierd. Its more standard to have a tabBarController that switches views and some of those views may be navigation controllers. But ...

    Create the UITabBarController and push it.

    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
    
    // create someView
    [viewControllers addObject:someView];
    // create someView2
    [viewControllers addObject:someView2];
    
    
    UITabBarController *tabController = [[UITabBarController alloc] init];
    [tabController setViewControllers:viewControllers];
    
    [[self navigationController] pushViewController:tabController animated:YES];
    

    Then, from the tabBarContoller view, based on some action, you can choose to pop it:

    [self.navigationController popViewControllerAnimated: NO];