码迷,mamicode.com
首页 > 其他好文 > 详细

获取 当前显示的 UIViewController

时间:2016-03-24 18:21:59      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

UIViewController * findBestVC(UIViewController *vc) {
    if (vc.presentedViewController) {
        return findBestVC(vc.presentedViewController);
    } else if ([vc isKindOfClass:[UISplitViewController class]]) {
        UISplitViewController *sp = (UISplitViewController *)vc;
        return sp.viewControllers.count > 0 ? findBestVC(sp.viewControllers.lastObject) : vc;
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        UINavigationController *na = (UINavigationController *)vc;
        return na.viewControllers.count > 0 ? findBestVC(na.topViewController) : vc;
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        UITabBarController *ta = (UITabBarController *)vc;
        return ta.viewControllers.count > 0 ? findBestVC(ta.selectedViewController) : vc;
    }
    return vc;
}

UIViewController * currentVC() {
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    return findBestVC(rootVC);
}

只需要调用

UIViewController *VC = currentVC();

 

获取 当前显示的 UIViewController

标签:

原文地址:http://www.cnblogs.com/shenhongbang/p/5316449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!