Skip to content

Instantly share code, notes, and snippets.

@pkh
Last active October 5, 2020 13:18
Show Gist options
  • Save pkh/c2e44eb6a11d62b83977 to your computer and use it in GitHub Desktop.
Save pkh/c2e44eb6a11d62b83977 to your computer and use it in GitHub Desktop.
Helpful method to dismiss ALL view controllers presented on top of a root view/navigation controller.
/**
Helpful method to dismiss ALL view controllers presented on top of a
root view/navigation controller.
This method is useful if you have a variable number of view controllers
that you'd need to dimiss to return to a base view. Used for my purposes
from the AppDelegate class during the process of handling the opening of
a URL scheme.
This method also relies on implementing a category on UIViewController
(or whatever your base view controller subclass is) to find the current
topmost view controller. These methods are included at the bottom of
this gist.
Usage:
[self dismissAllModalAndCoveringViewControllers]
*/
- (NSInteger)findRootViewController:(UIViewController *)viewController
{
if (viewController) {
return 1 + [self findRootViewController:viewController.presentingViewController];
}
return 0;
}
- (void)dismissAllModalAndCoveringViewControllers
{
UIViewController *currentViewController = [UIViewController currentViewController];
NSInteger numVCToDismiss = [self findRootViewController:currentViewController];
if (numVCToDismiss > 1) {
[currentViewController dismissViewControllerAnimated:YES completion:^{
[self dismissAllSignInAndTourViewControllers];
}];
}
}
/////////////////////////////////////////////////////////////////////////////////////
/**
Implement these methods in a category on UIViewController
These methods taken from this post on Stack Overflow:
http://stackoverflow.com/questions/24825123/get-the-current-view-controller-from-the-app-delegate
*/
+ (UIViewController *)findBestViewController:(UIViewController *)vc
{
if (vc.presentedViewController) {
// Return presented view controller
return [UIViewController findBestViewController:vc.presentedViewController];
} else if ([vc isKindOfClass:[UISplitViewController class]]) {
// Return right hand side
UISplitViewController* svc = (UISplitViewController*) vc;
if (svc.viewControllers.count > 0)
return [UIViewController findBestViewController:svc.viewControllers.lastObject];
else
return vc;
} else if ([vc isKindOfClass:[UINavigationController class]]) {
// Return top view
UINavigationController* svc = (UINavigationController*) vc;
if (svc.viewControllers.count > 0)
return [UIViewController findBestViewController:svc.topViewController];
else
return vc;
} else if ([vc isKindOfClass:[UITabBarController class]]) {
// Return visible view
UITabBarController* svc = (UITabBarController*) vc;
if (svc.viewControllers.count > 0)
return [UIViewController findBestViewController:svc.selectedViewController];
else
return vc;
} else {
// Unknown view controller type, return last child view controller
return vc;
}
}
+ (UIViewController *)currentViewController
{
UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
return [UIViewController findBestViewController:viewController];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment