美文网首页
获取当前VC

获取当前VC

作者: ADreamClusive | 来源:发表于2022-01-18 09:42 被阅读0次
+ (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