Created
May 29, 2015 03:06
-
-
Save zld/ab535a40fb53a0c2d309 to your computer and use it in GitHub Desktop.
Get the top ViewController from current View.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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