美文网首页
获取当前屏幕显示的viewcontroller

获取当前屏幕显示的viewcontroller

作者: ios_暗夜行者 | 来源:发表于2021-08-12 10:08 被阅读0次

    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;

                UIViewController*topVC = appRootVC;

                UIViewController*currentVC =nil;

                if(topVC.presentedViewController) {

                    topVC = topVC.presentedViewController;

                    NSLog(@"数组:%@", topVC.childViewControllers);

                    NSLog(@"模态出最外层的视图%@", [topVC.childViewControllers lastObject]);

                    //有模态情况下的根视图

                    currentVC = [topVC.childViewControllers lastObject];

                }else{

                    //获取非模态情况下的根视图

                    currentVC = [[ValuePass shareValuePass]getCurrentVC];

                }

    //获取当前屏幕显示的viewcontroller

    - (UIViewController *)getCurrentVC {

        UIViewController*result =nil;

        UIWindow * window = [[UIApplication sharedApplication] keyWindow];

        if (window.windowLevel != UIWindowLevelNormal) {

            NSArray*windows = [[UIApplicationsharedApplication]windows];

            for(UIWindow* tmpWininwindows) {

                if (tmpWin.windowLevel == UIWindowLevelNormal) {

                    window = tmpWin;

                    break;

                }

            }

        }

        UIView*frontView = [[windowsubviews]objectAtIndex:0];

        idnextResponder = [frontViewnextResponder];

        if([nextResponderisKindOfClass:[UIViewControllerclass]]) {

            result = nextResponder;

        }else{

            result = window.rootViewController;

        }

        /*

         *  在此判断返回的视图是不是你的根视图--我的根视图是tabbar

         */

        if ([result isKindOfClass:[DWStoreTabBarController class]]) {

            DWStoreTabBarController *mainTabBarVC = (DWStoreTabBarController *)result;

            result = [mainTabBarVCselectedViewController];

            result = [result.childViewControllerslastObject];

        }

        NSLog(@"非模态视图%@", result);

        returnresult;

    }

    相关文章

      网友评论

          本文标题:获取当前屏幕显示的viewcontroller

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