@interface AppDelegate (visibleVC)
// 获取当前展示的vc
- (UIViewController *)visibleViewController;
@end
@implementation AppDelegate (visibleVC)
- (UIViewController *)visibleViewController {
UIViewController *rootViewController = self.window.rootViewController;
return [self getVisibleViewControllerFrom:rootViewController];
}
-(UIViewController *) getVisibleViewControllerFrom:(UIViewController *) vc {
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self getVisibleViewControllerFrom:[((UINavigationController *) vc) visibleViewController]];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
return [self getVisibleViewControllerFrom:[((UITabBarController *) vc) selectedViewController]];
} else {
if (vc.presentedViewController) {
return [self getVisibleViewControllerFrom:vc.presentedViewController];
} else {
return vc;
}
}
}
@end