2

I am trying to use popToViewController and it I keep getting the error "Tried to pop to a view controller that doesn't exist"?

I am in a Settings view and when the user clicks "Sign Out" I dismiss the Settings VC and segue back to the mainView where an unwind segue method is called. In the unwind segue method I call the following.

-(IBAction)endSettingsViaLogout:(UIStoryboardSegue *)segue {

//[self performSegueWithIdentifier:@"mainToLoginSegue" sender:self];

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
//[self.navigationController popViewControllerAnimated:YES];

DLog(@"User finished with search");
}

When the poptoVC is called I get the "Tried to pop to a view controller that doesn't exist".

I NSLog the self.navigationController.viewControllers and I can see the stack of VC's and the one I want to pop to is in there?

/// UPDATE //////

Ok here is what I have found. If my segue to Settings is a regular "push" segue then the code works and I get popped back to where I want. If I do a custom segue, having come from the left side of the screen then it stops working. Even with the custom segue the self.navigationcontroller.viewcontrollers shows its in the stack. So why can't I pop back to it? Or how can I pop back to it with the custom segue?

jdog
  • 10,351
  • 29
  • 90
  • 165
  • This highly depends on what your custom segue is doing with the stack as well as whether the view controller is still in the stack when you call `popToViewController:` When is the segue called? Before or after your popToViewController? If the segue already takes the user back to the main screen, why are you attempting to pop the view controller? Some more code detailing your segue and sequence of events would help greatly. – Chris Dec 04 '13 at 11:23

4 Answers4

0

Do I get the view controller hierarchy right?

  • On your basis UINavigationViewController you have set a main view controller as root view controller.
  • Then a settings view controller has been pushed upon this.
  • Via "Sign out" the settings view controller is being segued back to the main view controller.

If so, you are actually trying pop "back" to a view controller, that does not exist, since you already have reached the root view controller of your navigation controller stack. In this case, all previously initialized controllers have been popped from the stack and you would have to reinitialize and push the desired view controller.

If I am missing some important point, it would be helpful, if you would describe your actual view controller stack at the moment the "Sign out" option is available. Furthermore, what exactly is printed on the console if you log the self.navigationController.viewControllers array?

f26e
  • 115
  • 6
0

well that basically tells objectatindex 1 does not exist:

things to try:

objectatindex:0

or

nslog(@"%d",[[self.navigationController.viewControllers]count]);//add it before the popline and see if it works

if its the topview controller then try below:

[navigationController topViewController] instead

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Jatin
  • 1,668
  • 2
  • 16
  • 23
  • Please see my last sentence. The view controller I want is there and it exists. Doing "po self.navigationController.viewcontrollers" I see it in the list. I can do [self.navigationController.viewcontrollers objectAtIndex:1] and its there. – jdog Jun 17 '13 at 00:51
  • when you doa custom segue...are you doing by using self.navigationcontroller? – Jatin Jun 17 '13 at 01:18
0
NSArray *viewControllers = [[self navigationController] viewControllers];

for( int i=0;i<[viewControllers count];i++)
{
    id obj=[viewControllers objectAtIndex:[viewControllers count]-i-1];
    if([obj isKindOfClass:[OrderCheckOutViewController class]]){

    [[self navigationController] popToViewController:obj animated:YES];
        return;
    }
}
Community
  • 1
  • 1
0

you can use the snippet to pop out to targetVC's next viewController in the navigationController's stack.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
BOOL findIt = NO;
UIViewController *targetVC = nil;
for (UIViewController *subVC in self.navigationController.viewControllers) {
    if (findIt) {
        break;
    }
    if (subVC == xxx) {
        findIt = YES;
    }else{
        targetVC = subVC;
    }
}
[self.navigationController popToViewController:targetVC animated:NO];
});
brownfeng
  • 41
  • 3