美文网首页
寻找最上层的控制器或者nextResponder

寻找最上层的控制器或者nextResponder

作者: 守护地中海的花 | 来源:发表于2021-07-03 19:10 被阅读0次

    上代码

    - (UIViewController *)findCurrentViewController {
        UIWindow *window = [[UIApplication sharedApplication].delegate window];
        UIViewController *topViewController = [window rootViewController];
        while (true) {
            if (topViewController.presentedViewController) {
                topViewController = topViewController.presentedViewController;
            } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
                topViewController = [(UINavigationController *)topViewController topViewController];
            } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
                UITabBarController *tab = (UITabBarController *)topViewController;
                topViewController = tab.selectedViewController;
            } else {
                break;
            }
        }
        return topViewController;
    }
    // 发现当前view的Controller
    
    - (UIViewController *)findSuperViewController:(UIView *)view
    {
        UIResponder *responder = view;
        // 循环获取下一个响应者,直到响应者是一个UIViewController类的一个对象为止,然后返回该对象.
        while ((responder = [responder nextResponder])) {
            if ([responder isKindOfClass:[UIViewController class]]) {
                return (UIViewController *)responder;
            }
        }
        return nil;
    }
    

    下面可以不看☺️

    presentedViewController: 是否还有present出去的控制器

    场景就是从HomePageController的一个按钮点击过去的
    homePage 是导航控制器管理的 导航控制器是由Tabbar控制器的
    模拟场景:homePageController 点击跳转BBBBViewController 然后点击BBBBViewController空白处跳转CCCViewController

    从homePageController 点击按钮 present 出BBBBViewController 打印

    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> (null)
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <DTBaseNavigationController: 0x107899200> (null)
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <HomePageController: 0x107897a00> (null)
    ------ HomePageNaviView.m ------ 46 行 ------ 最终:<HomePageController: 0x107897a00>
    

    跳转BBBBViewController 后打印

    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> <BBBBViewController: 0x1077f2ce0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f2ce0> (null)
    ------ HomePageNaviView.m ------ 49 行 ------ 最终:<BBBBViewController: 0x1077f2ce0>
    

    虽然是HomePageController present 但是寻找控制器的时候 TabBarController 就找到了BBBBViewController

    跳转BBBBViewController 然后点击CCCViewController 空白处 打印

    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> <BBBBViewController: 0x1077f2ce0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f2ce0> <CCCViewController: 0x10758f230>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10758f230> (null)
    

    重复打印就是如此

    ------ BBBBViewController.m ------ 24 行 ------ touchesBegan BBBB
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> <BBBBViewController: 0x1077f2ce0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f2ce0> <CCCViewController: 0x10758f230>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10758f230> <BBBBViewController: 0x1077f0610>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f0610> (null)
    ------ CCCViewController.m ------ 24 行 ------ touchesBegan cccc
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> <BBBBViewController: 0x1077f2ce0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f2ce0> <CCCViewController: 0x10758f230>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10758f230> <BBBBViewController: 0x1077f0610>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f0610> <CCCViewController: 0x114f0d710>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x114f0d710> (null)
    ------ BBBBViewController.m ------ 24 行 ------ touchesBegan BBBB
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> <BBBBViewController: 0x1077f2ce0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f2ce0> <CCCViewController: 0x10758f230>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10758f230> <BBBBViewController: 0x1077f0610>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f0610> <CCCViewController: 0x114f0d710>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x114f0d710> <BBBBViewController: 0x114d16a20>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x114d16a20> (null)
    ------ CCCViewController.m ------ 24 行 ------ touchesBegan cccc
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> <BBBBViewController: 0x1077f2ce0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f2ce0> <CCCViewController: 0x10758f230>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10758f230> <BBBBViewController: 0x1077f0610>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f0610> <CCCViewController: 0x114f0d710>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x114f0d710> <BBBBViewController: 0x114d16a20>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x114d16a20> <CCCViewController: 0x10775faa0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10775faa0> (null)
    ------ BBBBViewController.m ------ 24 行 ------ touchesBegan BBBB
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> <BBBBViewController: 0x1077f2ce0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f2ce0> <CCCViewController: 0x10758f230>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10758f230> <BBBBViewController: 0x1077f0610>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f0610> <CCCViewController: 0x114f0d710>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x114f0d710> <BBBBViewController: 0x114d16a20>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x114d16a20> <CCCViewController: 0x10775faa0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10775faa0> <BBBBViewController: 0x107549a40>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x107549a40> (null)
    ------ CCCViewController.m ------ 24 行 ------ touchesBegan cccc
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> <BBBBViewController: 0x1077f2ce0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f2ce0> <CCCViewController: 0x10758f230>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10758f230> <BBBBViewController: 0x1077f0610>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f0610> <CCCViewController: 0x114f0d710>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x114f0d710> <BBBBViewController: 0x114d16a20>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x114d16a20> <CCCViewController: 0x10775faa0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10775faa0> <BBBBViewController: 0x107549a40>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x107549a40> <CCCViewController: 0x107580940>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x107580940> (null)
    ------ BBBBViewController.m ------ 24 行 ------ touchesBegan BBBB
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <TabBarController: 0x107872c00> <BBBBViewController: 0x1077f2ce0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f2ce0> <CCCViewController: 0x10758f230>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10758f230> <BBBBViewController: 0x1077f0610>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x1077f0610> <CCCViewController: 0x114f0d710>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x114f0d710> <BBBBViewController: 0x114d16a20>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x114d16a20> <CCCViewController: 0x10775faa0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x10775faa0> <BBBBViewController: 0x107549a40>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x107549a40> <CCCViewController: 0x107580940>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <CCCViewController: 0x107580940> <BBBBViewController: 0x10753e2e0>
    ------ UIViewController+DTCategory.m ------ 28 行 ------ <BBBBViewController: 0x10753e2e0> (null)
    
    

    下面是我之前写的 没有考虑先判断presentedViewController

    + (UIViewController *)topViewController
    {
        return [self topVCWithRootVC:self.keyWindow.rootViewController];
    }
    
    + (UIViewController *)topVCWithRootVC:(UIViewController *)rootVC
    {
        NSLog(@"%@ %@",rootVC,rootVC.presentedViewController);
        if ([rootVC isKindOfClass:[UITabBarController class]])
        {
            UITabBarController *tabBarController = (UITabBarController *)rootVC;
            return [self topVCWithRootVC:tabBarController.selectedViewController];
        }
        else if ([rootVC isKindOfClass:[UINavigationController class]])
        {
            UINavigationController *navigationController = (UINavigationController *)rootVC;
            return [self topVCWithRootVC:navigationController.visibleViewController];
        }
        else if (rootVC.presentedViewController)
        {
            UIViewController *presentedViewController = rootVC.presentedViewController;
            return [self topVCWithRootVC:presentedViewController];
        }
        else
        {
            return rootVC;
        }
    }
    
    

    打印一波

    ------ UIApplication+Extension.m ------ 36 行 ------ <TabBarController: 0x112095a00> (null)
    ------ UIApplication+Extension.m ------ 36 行 ------ <DTBaseNavigationController: 0x112096800> (null)
    ------ UIApplication+Extension.m ------ 36 行 ------ <HomePageController: 0x112087000> (null)
    ------ HomePageNaviView.m ------ 47 行 ------ 最终:<HomePageController: 0x112087000>
    ------ UIApplication+Extension.m ------ 36 行 ------ <TabBarController: 0x112095a00> (null)
    ------ UIApplication+Extension.m ------ 36 行 ------ <DTBaseNavigationController: 0x112096800> (null)
    ------ UIApplication+Extension.m ------ 36 行 ------ <HomePageController: 0x112087000> (null)
    ------ HomePageNaviView.m ------ 49 行 ------ 111111111
    ------ UIApplication+Extension.m ------ 36 行 ------ <TabBarController: 0x112095a00> <BBBBViewController: 0x111d4ad60>
    ------ UIApplication+Extension.m ------ 36 行 ------ <DTBaseNavigationController: 0x112096800> <BBBBViewController: 0x111d4ad60>
    ------ UIApplication+Extension.m ------ 36 行 ------ <BBBBViewController: 0x111d4ad60> (null)
    ------ HomePageNaviView.m ------ 50 行 ------ 最终:<BBBBViewController: 0x111d4ad60>
    

    打印nextResponder

    如果view就是keywindow 打印的结果是
    UIApplication、AppDelegate
    
    
    如果view 是一个普通view 在控制器上 则是
    xxxController 然后控制器的nextResponder 就是null
    

    相关文章

      网友评论

          本文标题:寻找最上层的控制器或者nextResponder

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