美文网首页
获取当前VC的探究

获取当前VC的探究

作者: blueshadow | 来源:发表于2017-03-31 11:20 被阅读30次

基本思路

如果界面已经有了UITabBarController,则可以先获取当前tab对应的UINavigationController,再获取它的最顶层vc

UINavigationController *navi = [self.tabBarController selectedViewController];
UIViewController *topVC = navi.topViewController;

那么topVC就是最前VC了么?topVC其实还有可能是UITabBarController

    if([topVC isKindOfClass:[UITabBarController class]]){
        UITabBarController *tabVC = (UITabBarController *)topVC;
        currentVC= tabVC.selectedViewController;
    }
    else{
        if(currentVC.presentedViewController){
            currentVC = currentVC.presentedViewController;
        }
    }
    if([currentVC isKindOfClass:[UINavigationController class]]){
        UINavigationController *naviVC = (UINavigationController *)currentVC;
        currentVC = naviVC.topViewController;
    }
    return NSStringFromClass([currentVC class]);

NavigationController可能不止一个

相关文章

网友评论

      本文标题:获取当前VC的探究

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