Hi I am developing an application in which I supposed to go:
UIViewController
1 to UIViewController
2UIViewController
2 to UIViewController
3UIViewController
3 to UIViewController
4UIViewController
4 back to UIViewController
2I 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 UIViewController
4 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 UIViewController
2
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 UIViewController
2 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
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];
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];
}
}
Try this line to pop your viewcontroller to 2 nd
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With