Search code examples
iosobjective-cuiviewcontrolleruinavigationcontrollerecslidingviewcontroller

ECSliding ViewController Navigation Hierarchy


I'm using ECSlidingViewController (See Documentation) to achieve the sliding menu function.

What I intend to do

Here's the list of my ViewControllers

  • SlidingMenu
  • View Controller A (embedded in Navigation Controller)
  • View Controller B1
  • View Controller B2
  • View Controller C

The navigation sequence should like this:

A----------B1
    |      |-------C 
    |
    |------B2

Also inside SlidingMenu, there are options to go to ViewController B1 or ViewController B2 directly.

The effect I want to achieve is: when using SlidingMenu to navigate to ViewController B1 or ViewController B2, the Back button on the navigation bar should take the view back to their parent view. (i.e, B1/B2 should back to A).

Problem

When the app is running, only after using the ECSlidingViewController to navigate to B1/B2, the navigation bar on B1/B2 is missing and there's no "Back" button to get to their parent view (i.e. view A).

Please note: If B1/B2 is navigated via view A, then the navigation bar is fine and well displayed.

So, how can I achieve the desired effect? Is there any method to push the correct navigation stack in it rather than just replace the topViewController?

Related Code

Inside the SlidingMenu view, there are two buttons for navigating to B1 and B2

- (IBAction)B1Pressed:(id)sender {
    self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"B1"];
    [self.slidingViewController resetTopViewAnimated:YES];
}

- (IBAction)B2Pressed:(id)sender {
    self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"B2"];
    [self.slidingViewController resetTopViewAnimated:YES];
}

Hope I can use these screenshots to elaborate the problem clearly:

Sliding Menu:

enter image description here

View A (top left button is to open the Sliding Menu)

enter image description here

View B1 (if navigate from View A, the navigation bar and "back" button are well displayed)

enter image description here

View B1 (if navigate from Sliding Menu, the navigation bar and "back" button are disappeared)

enter image description here


Solution

  • This code is your problem:

    - (IBAction)B1Pressed:(id)sender {
        self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"B1"];
        [self.slidingViewController resetTopViewAnimated:YES];
    

    If you think about what this is doing it's destroying the centre navigation controller and the A controller that it contains (and anything else in the stack).

    What you should be doing, instead of calling topViewController =, is to get the viewControllers from the navigation controller (which is the topViewController) and edit the array contents (and reset it into the navigation controller.

    In this way you maintain the navigation and root view controller (A) while inserting your new controller into the stack.