其他分享
首页 > 其他分享> > iOS getCurrentVC getCurrentUIVC

iOS getCurrentVC getCurrentUIVC

作者:互联网

/// 获取最顶层控制器 
-(UIViewController *)getCurrentVC{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal){
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows){
            if (tmpWin.windowLevel == UIWindowLevelNormal){
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    return result;
}

/// 获取当前UI最顶层控制器 可以通过该控制器push到下一页
-(UIViewController *)getCurrentUIVC{
    UIViewController  *superVC = [self kzgetCurrentVC];
    
    if ([superVC isKindOfClass:[UITabBarController class]]) {
        UIViewController  *tabSelectVC = ((UITabBarController*)superVC).selectedViewController;
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }else
        if ([superVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController*)superVC).viewControllers.lastObject;
        }
    return superVC;
}

标签:tabSelectVC,return,iOS,getCurrentVC,getCurrentUIVC,window,superVC,result,UIViewC
来源: https://www.cnblogs.com/qqcc1388/p/16354702.html