Search code examples
iosuitabbarcontrolleruitabbar

Can I change the first screen of tab item programatically?


Can I change the first screen of a tab item programatically?

For example,

When user is in Tab A, upon tapping a button there, user will be navigated to Tab C with Screen 1 However, when user is in Tab C, upon tapping a button here, user will be navigated to Tab C with Screen 2. Therefore, both screen 1 and 2 will be the first screen of Tab C depend on where the user comes from.

Can I achieve something like that? Thanks!


Solution

  • Based on this post How to replace a navigation root controller by tab bar controller in existing iphone app , can change the view controllers by this code:

    NSMutableArray * viewControllers = [[NSMutableArray alloc]init];
    
    
    FirstViewController * firstViewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
    UINavigationController * nvc = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    [firstViewController release];
    [viewControllers addObject:nvc];
    [nvc release];
    
    SecondViewController * secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
     nvc = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    [secondViewController release];
    [viewControllers addObject:nvc];
    [nvc release];
    
    UITabBarController * tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = viewControllers;
    [window addSubview:tabBarController.view];