Last active
February 24, 2016 03:28
-
-
Save yimingtang/7132252 to your computer and use it in GitHub Desktop.
Find the topmost view controller
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+Topmost.h | |
// | |
// Created by Yiming Tang on 13-10-14. | |
// Copyright (c) 2013 Yiming Tang. All rights reserved. | |
// | |
@interface UIViewController (Topmost) | |
+ (UIViewController *)topmostViewController; | |
+ (UIViewController *)topViewControllerWithRootViewController:(UIViewController *)rootViewController; | |
@end |
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+Topmost.m | |
// | |
// Created by Yiming Tang on 13-10-14. | |
// Copyright (c) 2013 Yiming Tang. All rights reserved. | |
// | |
#import "UIViewController+Topmost.h" | |
@implementation UIViewController (Topmost) | |
+ (UIViewController *)topmostViewController | |
{ | |
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController; | |
return [self topViewControllerWithRootViewController:rootViewController]; | |
} | |
+ (UIViewController *)topViewControllerWithRootViewController:(UIViewController *)rootViewController | |
{ | |
if ([rootViewController isKindOfClass:[UITabBarController class]]) { | |
UITabBarController *tabBarController = (UITabBarController *)rootViewController; | |
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController]; | |
} | |
else if ([rootViewController isKindOfClass:[UINavigationController class]]) { | |
UINavigationController *navigationController = (UINavigationController *)rootViewController; | |
return [self topViewControllerWithRootViewController:navigationController.visibleViewController]; | |
} | |
else if (rootViewController.presentedViewController) { | |
UIViewController *presentedViewController = rootViewController.presentedViewController; | |
return [self topViewControllerWithRootViewController:presentedViewController]; | |
} | |
else { | |
return rootViewController; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment