Created
July 13, 2018 08:57
-
-
Save wzshare/b54915d9bdccbbfd9b3db2ddb89c5102 to your computer and use it in GitHub Desktop.
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
@implementation UIViewController (AlertObject) | |
- (UIViewController *)visibleViewControllerIfExist { | |
if (self.presentedViewController) { | |
return [self.presentedViewController visibleViewControllerIfExist]; | |
} | |
if ([self isKindOfClass:[UINavigationController class]]) { | |
return [((UINavigationController *)self).topViewController visibleViewControllerIfExist]; | |
} | |
if ([self isKindOfClass:[UITabBarController class]]) { | |
return [((UITabBarController *)self).selectedViewController visibleViewControllerIfExist]; | |
} | |
if ([self isViewLoaded] && self.view.window) { | |
return self; | |
} else { | |
NSLog(@"visibleViewControllerIfExist:,找不到可见的viewController。self = %@, self.view.window = %@", self, self.view.window); | |
return nil; | |
} | |
} | |
+ (UIViewController *)visibleViewController { | |
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController; | |
UIViewController *visibleViewController = [rootViewController visibleViewControllerIfExist]; | |
return visibleViewController; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment