美文网首页
2020-04-08 获取当前显示的 UIViewControl

2020-04-08 获取当前显示的 UIViewControl

作者: 小专注 | 来源:发表于2020-04-08 13:36 被阅读0次

获取当前显示的 UIViewController

@return 当前VC

*/

+ (UIViewController *)getCurrentViewController;

+ (UIViewController *)getCurrentViewController {

    UIWindow *window = [[UIApplication sharedApplication].delegate window];

    UIViewController *topViewController = [window rootViewController];

    while (true) {

        if (topViewController.presentedViewController) {

            topViewController = topViewController.presentedViewController;

        } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {

            topViewController = [(UINavigationController *)topViewController topViewController];

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

            UITabBarController *tab = (UITabBarController *)topViewController;

            topViewController = tab.selectedViewController;

        } else {

            break;

        }

    }

    return topViewController;

}

相关文章

网友评论

      本文标题:2020-04-08 获取当前显示的 UIViewControl

      本文链接:https://www.haomeiwen.com/subject/aictmhtx.html