Search code examples
iosobjective-cuinavigationcontrollerpushviewcontrollerpoptoviewcontroller

pop To View Controller


I am new to coding objective-C and am trying to use pop to viewController. To do this when a button is tapped i use

[self.navigationController popToViewController:(what goes here) animated:YES];

I know its a UIViewController* that goes there but I am wondering where do I declare this and what code would I use to declare it.

My basic storyboard is I have 4 view controller A,B,C,D and A is the root which pushed to B which pushes to C which pushes to D, my button is on D and I am trying to pop back to B. The object at index method won't work because its doesn't always go A->B->C->D sometimes it goes A->C->B->D so

[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex:2] animated:YES];

So that doesn't work.

Thanks for the help in advance. Sorry if this question is too basic.


Solution

  • Personally I don't find this as a basic question, even I got struck in such a situation. But I found a good solution by using “Mediator Design pattern” and develop my custom UINavigationController as a coordinator by combining the method:

    - (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
    

    and maintaining my own navigation stack.

    A coordinator (your custom UINavigationController) has to have the control over your navigation, if your are having unpredictable way of navigation.