How to push additional view controllers onto Navig

2019-07-17 20:05发布

问题:

I have an app that starts with a basic view controller at the root of my navigation controller

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.navController.viewControllers = [NSArray arrayWithObject:[self getHomeViewController]];

    [[self getWindow] addSubview:self.navController.view];
    [[self getWindow] makeKeyAndVisible];

    return YES;
}

Once a navigation item is selected I push a tab bar controller w/ a few view controllers.

-(void)launchOptionWithTabBarController:(NSUInteger)selectedIndex
{
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:
                                             [self getFirstViewController], 
                                             [self getSecondViewController],
                                             nil];

    [self.tabBarController setSelectedIndex:selectedIndex];
    [self.navController pushViewController:self.tabBarController animated:YES];
}

The problem is that I now need to add another view controller on top of the one open in the tab bar controller ... and when I push one to the navigation controller like so ..

- (void)launchNewViewControllerWithArray:(NSArray *)stuff
{
    self.newViewController = [self getNewViewController];

    [self.navController pushViewController:self.newViewController animated:YES];
}

I no longer see the tab bar navigation (and I would prefer to see this nav w/ each item I push).

How can I modify my control flow so the tab bar nav items stay along the bottom?

回答1:

You need to parent the navigation controller in the tabbar controller instead of vice versa. Or introduce another navigation controller parented on your tabbar controller, and push the new viewcontroller onto that.



回答2:

You can achieve this by pushing new viewController on tabbar selected viewcontroller. so you can write your launch function like this

- (void)launchNewViewControllerWithArray:(NSArray *)stuff
{
    self.newViewController = [self getNewViewController];

    [[self.tabbarController selectedViewController] pushViewController:self.newViewController animated:YES];
}