iOS - 获取当前展示的VC

2020-04-03  本文已影响0人  HanZhiZzzzz

/** 递归查找当前显示的VC*/

+ (UIViewController *)recursiveFindCurrentShowViewControllerFromViewController:(UIViewController *)fromVC

{

    if ([fromVC isKindOfClass:[UINavigationController class]]) {

        return [self recursiveFindCurrentShowViewControllerFromViewController:[((UINavigationController *)fromVC) visibleViewController]];

    } else if ([fromVC isKindOfClass:[UITabBarController class]]) {

        return [self recursiveFindCurrentShowViewControllerFromViewController:[((UITabBarController *)fromVC) selectedViewController]];

    } else {

        if (fromVC.presentedViewController) {

            return [self recursiveFindCurrentShowViewControllerFromViewController:fromVC.presentedViewController];

        } else {

            return fromVC;

        }

    }

}

/** 查找当前显示的ViewController*/

+ (UIViewController *)getCurrentShowViewController

{

    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;

    UIViewController *currentShowVC = [self recursiveFindCurrentShowViewControllerFromViewController:rootVC];

    return currentShowVC;

}

摘自:https://blog.csdn.net/weixin_34242509/article/details/93697460

上一篇下一篇

猜你喜欢

热点阅读