得到当前活动的controller
UIWindow+PazLabs.h (header file)
#import <UIKit/UIKit.h> @interface UIWindow (PazLabs) - (UIViewController *) visibleViewController; @end
UIWindow+PazLabs.m (implementation file)
#import "UIWindow+PazLabs.h" @implementation UIWindow (PazLabs) - (UIViewController *)visibleViewController { UIViewController *rootViewController = self.rootViewController; return [UIWindow getVisibleViewControllerFrom:rootViewController]; } + (UIViewController *) getVisibleViewControllerFrom:(UIViewController *) vc { if ([vc isKindOfClass:[UINavigationController class]]) { return [UIWindow getVisibleViewControllerFrom:[((UINavigationController *) vc) visibleViewController]]; } else if ([vc isKindOfClass:[UITabBarController class]]) { return [UIWindow getVisibleViewControllerFrom:[((UITabBarController *) vc) selectedViewController]]; } else { if (vc.presentedViewController) { return [UIWindow getVisibleViewControllerFrom:vc.presentedViewController]; } else { return vc; } } } @end