Skip to content

Instantly share code, notes, and snippets.

@raza08
Forked from snikch/gist:3661188
Created December 30, 2017 06:50
Show Gist options
  • Select an option

  • Save raza08/ff3c1d4056f7fcbd178a996b0f0285e3 to your computer and use it in GitHub Desktop.

Select an option

Save raza08/ff3c1d4056f7fcbd178a996b0f0285e3 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