1

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.

  • 1
    Is your question about pushing, popping, or how to make a view controller? – MJN May 23 '14 at 21:43
  • If view controller B is in the navigation stack the popToViewController is the right method. You either need to keep a reference to view controller B, say as a property of your app delegate or you can iterate through the navigation controllers viewcontrollerwithrestorationidentifier array to identify the reference you need - perhaps using isMemberOf class method – Paulw11 May 23 '14 at 22:04
  • If you have a storyboard, you should not be calling `pushViewController` at all. You should be using a push segue. – matt May 23 '14 at 22:04
  • @MJN it is about popping sorry about that typo, how would I go about save it in the app delegate as a property? how do I save a reference of it? thanks –  May 23 '14 at 23:05
  • If you've made a typo, please update your question. – MJN May 23 '14 at 23:18

2 Answers2

1

You need a way to find the desired view controller to pop to.

-(IBAction)popToDesiredViewController:(id)sender
{
    UIViewController *desiredVC = nil;

    // LOOK AT ALL VIEW CONTROLLERS IN NAVIGATION CONTROLLER
    for (UIViewController *oneVC in self.navigationController.viewControllers) {

        // CHECK IF THIS IS THE VIEW CONTROLLER YOU WANT
        // change this to your logic
        BOOL foundDesiredVC = [oneVC isKindOfClass: [SignInVC class]]; 

        if (foundDesiredVC) {
            desiredVC = oneVC;
            break;
        }
    }

    [self.navigationController popToViewController:desiredVC animated:YES];
}
MJN
  • 10,748
  • 1
  • 23
  • 32
  • Hey when I use that in the button pressed method I get this error "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'" and sorry about the typo –  May 24 '14 at 05:31
  • Is the desired VC nil every time that happens? If so you'll want to wrap the pop method in an if statement that only fires when the `desiredVC` exists. – MJN May 24 '14 at 05:37
  • I still get the error if I am logging out a user when the button is pushed and then loging them back in could that affect the stack, I would think it would because the view has been loaded?? –  May 24 '14 at 06:09
0

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.

Goppinath
  • 10,569
  • 4
  • 22
  • 45