Skip to content

Instantly share code, notes, and snippets.

@mrugeshtank
Forked from snikch/gist:3661188
Created October 24, 2017 13:49
Show Gist options
  • Save mrugeshtank/5ed4b5631f0c432356ba9c60b593b956 to your computer and use it in GitHub Desktop.
Save mrugeshtank/5ed4b5631f0c432356ba9c60b593b956 to your computer and use it in GitHub Desktop.
Find the current top view controller for your iOS application
- (UIViewController *)topViewController{
return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}
if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [self topViewController:lastViewController];
}
UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
return [self topViewController:presentedViewController];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment