-
-
Save xinnyu/7eed9321220c3a82bd111413d81dbf14 to your computer and use it in GitHub Desktop.
Find the current top view controller for your iOS application
This file contains hidden or 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 { | |
| UIViewController *resultVC; | |
| resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]]; | |
| while (resultVC.presentedViewController) { | |
| resultVC = [self _topViewController:resultVC.presentedViewController]; | |
| } | |
| return resultVC; | |
| } | |
| + (UIViewController *)_topViewController:(UIViewController *)vc { | |
| if ([vc isKindOfClass:[UINavigationController class]]) { | |
| return [self _topViewController:[(UINavigationController *)vc topViewController]]; | |
| } else if ([vc isKindOfClass:[UITabBarController class]]) { | |
| return [self _topViewController:[(UITabBarController *)vc selectedViewController]]; | |
| } else { | |
| return vc; | |
| } | |
| return nil; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment