Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[self.navigationController popToViewController:VC2 animated:NO]; crash

Hi I am developing an application in which I supposed to go:

  1. From UIViewController1 to UIViewController2
  2. From UIViewController2 to UIViewController3
  3. From UIViewController3 to UIViewController4
  4. From UIViewController4 back to UIViewController2

I am using UINavigationController. When I use [self.navigationController pushViewController:VC2 animated:NO]; and [self.navigationController popViewControllerAnimated:NO]; everything works fine.

But when I use [self.navigationController popToViewController:VC2 animated:NO]; from UIViewController4 application terminates saying Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'

Following is my code how I am popping to UIViewController2

for (UIViewController *vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[ViewController2 class]]) {
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
                ViewController2 *VC2 = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
                [self.navigationController popToViewController:VC2 animated:NO];
            }
        }

When I printed navigation array it shows UIViewController2 in stack. I have added UINavigationController from Editor->embed in->Navigation Controller

Can anyone please tell me why this is happening? I tried to search for this issue but nothing is helpful

like image 634
Shital Tiwari Avatar asked Aug 05 '14 09:08

Shital Tiwari


3 Answers

Here you instantiate a new VC2 view controller instance, this is not the instance you have on the navigation view controller stack!

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
            ViewController2 *VC2 = [storyboard     instantiateViewControllerWithIdentifier:@"ViewController2"];

So you have to find the right instance in

[[self.navigationController] viewControllers]

Solution 1: (as iDev said, jump to second view controller on stack, use this if you know it is the second one)

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

Solution 2: (generally go back 2 level on the stack)

NSUInteger ownIndex = [self.navigationController.viewControllers indexOfObject:self];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:ownIndex - 2] animated:YES];
like image 178
OliverD Avatar answered Nov 13 '22 21:11

OliverD


that is your code:

for (UIViewController *vc in self.navigationController.viewControllers) {
    if ([vc isKindOfClass:[ViewController2 class]]) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        ViewController2 *VC2 = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
        [self.navigationController popToViewController:VC2 animated:NO];
    }
}

you might have already spotted that the vc is a kind of ViewController2 and that is already in the navigation stack. the new instance, you created as VC2 of the ViewController2, is not in the navigation stack, it never was.

see the exception's body:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'

therefore you want to navigate back to a view controller which has not pushed into the navigation stack – that causes the crash.

you have to navigate back to a view controller which has been already in the stack – that is your vc:

for (UIViewController *vc in self.navigationController.viewControllers) {
    if ([vc isKindOfClass:[ViewController2 class]]) {
        [self.navigationController popToViewController:vc animated:NO];
    }
}
like image 9
holex Avatar answered Nov 13 '22 21:11

holex


Try this line to pop your viewcontroller to 2 nd

  [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
like image 5
Rajesh Avatar answered Nov 13 '22 21:11

Rajesh