Search code examples
iosobjective-cuinavigationcontrollerpushviewcontroller

Navigation Stack is nil after setViewControllers


Here is my hierarchy:

A - B - C - D (tabs)
|   |   |   |
|   |   |   |
----------------> AddEventViewController (can be called from any tabs)
    |
    |
    |----> SuggestFeedController (can be accessed from B only)

Let's imagine I'm currently in the D tab, and I call AddEventViewController. After this, I want to redirect the user to "SuggestFeedController", but I want to keep the hierarchy, it means that pressing the back button should redirect to B and not AddEventViewController nor D.

What I have tried to do is the following:

UITabBarController *tabbarController = ((UITabBarController *)appDelegate.window.rootViewController);
                 [tabbarController setSelectedIndex:1]; // Selecting the B tab

UIStoryboard* storyboard=  appDelegate.window.rootViewController.storyboard;

UICategoryFeedController *cfvC = [storyboard instantiateViewControllerWithIdentifier:@"UICategoryFeedController"]; // Instantiating B view Controller

SuggestFeed *suggestFeed = [storyboard instantiateViewControllerWithIdentifier:@"SuggestFeed"];


NSArray *viewControllers = [[NSArray alloc] initWithObjects:cfvC, suggestFeed, nil]; // Trying to create my own navigation stack, B first, then Suggest Feed


[self.navigationController pushViewController:suggestFeed animated:NO]; // Some guys on internet do the push, even if I don't think it's necessary because it's already in the stack


NSLog(@"nav stack1 : %@", self.navigationController.viewControllers );
/* returns stack1 : (
    "<D: 0xa627990>",
    "<AddEventViewController: 0x108cb7e0>",
    "<SuggestFeed: 0x123735f0>"
) */


[self.navigationController setViewControllers:viewControllers animated:NO];

NSLog(@"nav stack2 : %@", self.navigationController.viewControllers );
/* returns stack2 : (null) */

Am I doing it wrong? (stupid question)

Why is the stack2 nil, while I have set the viewControllers from my array?

If you can bring some help, thanks in advance :-)


Solution

  • Have you checked that cfvC is not nil? This could be the case if @"UICategoryFeedController" doesn't match an identifier in your storyboard. I ask because this would make viewControllers an empty array, which would explain the results of your logging.

    On another note, I'm guessing that you have a set-up where each tab in your UITabBarController contains its own navigation stack (i.e. there are four UINavigationController instances, set as the viewControllers of your tab bar controller). Secondly, I assume self in your example is view controller D. This means that whenever you call self.navigationController in this method, what you get back is the navigation controller in tab D, regardless of which tab is selected. To set the view controllers in tab B, you have to get a reference to the navigation controller in tab B. If I'm correct with my assumptions, you could do something like:

    UINavigationController *tabBNavigationController = tabbarController.viewControllers[1];
    

    Setting your viewControllers array on this navigation controller should do the trick. Also, you're right, you don't want to do the pushViewController bit.