Skip to content

Instantly share code, notes, and snippets.

@wzshare
Created July 13, 2018 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wzshare/b54915d9bdccbbfd9b3db2ddb89c5102 to your computer and use it in GitHub Desktop.
Save wzshare/b54915d9bdccbbfd9b3db2ddb89c5102 to your computer and use it in GitHub Desktop.
@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