获取当前VC
+ (UIViewController *)currentViewController
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *top = window.rootViewController;
while (top.presentedViewController) {
top = top.presentedViewController;
}
return [[self class] currentViewControllerWithRootViewController:top];
}
+ (UIViewController*)currentViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self currentViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self currentViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self currentViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
本文标题:获取当前VC
本文链接:https://www.haomeiwen.com/subject/ngqkhrtx.html
网友评论