美文网首页
iOS 获取当前控制器

iOS 获取当前控制器

作者: 小黑技术 | 来源:发表于2022-02-25 14:48 被阅读0次

    -(UIViewController *)getCurrentVC

    {

        UIViewController*result =nil;

        UIWindow* window = [[UIApplicationsharedApplication]keyWindow];

        if (window.windowLevel != UIWindowLevelNormal)

            {

                NSArray*windows = [[UIApplicationsharedApplication]windows];

                for(UIWindow* tmpWininwindows)

                    {

                        if(tmpWin.windowLevel==UIWindowLevelNormal)

                            {

                                window = tmpWin;

                                break;

                                }

                        }

                }

        //从根控制器开始查找

        UIViewController*rootVC = window.rootViewController;

        id nextResponder= [rootVC.viewnextResponder];

        if([nextResponderisKindOfClass:[UINavigationControllerclass]]) {

            result = ((UINavigationController*)nextResponder).topViewController;

            if([resultisKindOfClass:[UITabBarControllerclass]]) {

                result = ((UITabBarController*)result).selectedViewController;

                }

            }else if([nextResponderisKindOfClass:[UITabBarControllerclass]]) {

                result = ((UITabBarController*)nextResponder).selectedViewController;

                if([resultisKindOfClass:[UINavigationControllerclass]]) {

                    result = ((UINavigationController*)result).topViewController;

                    }

                }else if([nextResponderisKindOfClass:[UIViewControllerclass]]) {

                    result = nextResponder;

                    }else{

                        result = window.rootViewController;

                        if([resultisKindOfClass:[UINavigationControllerclass]]) {

                            result = ((UINavigationController*)result).topViewController;

                            if([resultisKindOfClass:[UITabBarControllerclass]]) {

                                result = ((UITabBarController*)result).selectedViewController;

                                }

                            }else if([resultisKindOfClass:[UIViewControllerclass]]) {

                                result = nextResponder;

                                }

                        }

        returnresult;

    }

    相关文章

      网友评论

          本文标题:iOS 获取当前控制器

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