Skip to content

Instantly share code, notes, and snippets.

@serhatsezer
Last active August 29, 2015 14:16
Show Gist options
  • Save serhatsezer/e4be0d635aa278a5ca51 to your computer and use it in GitHub Desktop.
Save serhatsezer/e4be0d635aa278a5ca51 to your computer and use it in GitHub Desktop.
Pop to specific ViewController in NavigationController
// Navigation Stack -> (pushed)
// Steps 1. AViewController -> 2. BViewController -> 3. CViewController
// in the CViewController user tapped button -> -backToFirstViewController
// backToFirstViewController
- (void)backToFirstViewController {
// get navigation stack as an array.
NSArray *viewControllers = self.navigationController.viewControllers;
// First way
// This way array goes back twice and find AViewController. This is obvious.
[self.navigationController popToViewController:viewControllers[viewControllers.count-2] animated:YES];
// Second way
for(UIViewController *currentVC in viewControllers) {
if([currentVC isKindOfClass:[AViewController class]]) {
[self.navigationController popToViewController:currentVC animated:YES];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment