获取当前VC的探究

基本思路

如果界面已经有了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的探究)