zoukankan      html  css  js  c++  java
  • 当前最上层的视图控制器vc 和 当前最上层的导航控制器nav------present----visible

    在处理 URL Router 跳转的时候,我们经常需要得到 当前最上层的视图控制器 和 当前最上层的导航控制器 来进行视图跳转或者方法调用。

    - (UIViewController *)currentViewController { UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; UIViewController *vc = keyWindow.rootViewController; while (vc.presentedViewController) { vc = vc.presentedViewController; if ([vc isKindOfClass:[UINavigationController class]]) { vc = [(UINavigationController *)vc visibleViewController]; } else if ([vc isKindOfClass:[UITabBarController class]]) { vc = [(UITabBarController *)vc selectedViewController]; } } return vc; } - (UINavigationController *)currentNavigationController { return [self currentViewController].navigationController; }




    /////////////////////////////////////////////////////////////////////////
    2个只读属性:
    presentedViewController:The view controller that is presented by this view controlller(read-only),被本视图控制器present出来的的视图控制器

    presentingViewController:The view controller that presented this view controller. (read-only),present出来本视图控制器的视图控制器

    如A-->弹出B, 则A.presentedViewController = B
    B.presentingViewController = A

    dismissViewControllerAnimated:YES
    Dismisses the view controller that was presented modally by the view controller.
    也就是在在A上调该方法,dismiss掉A弹出的vc
    如果在B上调,会调用presenting view的该方法,即A的该方法,很绕啊。


    UINavigationController 中有visibleViewControllertopViewController

    • visibleViewController 当前显示的控制器
    • topViewController 是某个导航栈的栈顶视图
    • visibleViewController跟导航栈没有关系,只是当前显示的控制器,也就是说任意一个导航的visibleViewController所返回的值应该是一样的,
    • topViewController 是某个导航栈的栈顶视图,和导航控制器相关
      换句话说如果在仅有一个导航栈上,visibleViewControllertopViewController应该是没有区别得。

    方法一 : 获取当前显示的控制器 UIWindow (Visible)

     - (UIViewController *)visibleViewController {
        UIViewController *rootViewController =[[[[UIApplication sharedApplication] delegate] window] rootViewController];
        return [UIWindow getVisibleViewControllerFrom:rootViewController];
    }
    + (UIViewController *) getVisibleViewControllerFrom:(UIViewController *) vc {
        if ([vc isKindOfClass:[UINavigationController class]]) {
            return [UIWindow getVisibleViewControllerFrom:[((UINavigationController *) vc) visibleViewController]];
        } else if ([vc isKindOfClass:[UITabBarController class]]) {
            return [UIWindow getVisibleViewControllerFrom:[((UITabBarController *) vc) selectedViewController]];
        } else {
            if (vc.presentedViewController) {
                return [UIWindow getVisibleViewControllerFrom:vc.presentedViewController];
            } else {
                return vc;
            }
        }
    
    }

    方法二 :

    - (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
        if ([rootViewController isKindOfClass:[UITabBarController class]]) {
            UITabBarController* tabBarController = (UITabBarController*)rootViewController;
            return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
        } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController* navigationController = (UINavigationController*)rootViewController;
            return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
        } else if (rootViewController.presentedViewController) {
            UIViewController* presentedViewController = rootViewController.presentedViewController;
            return [self topViewControllerWithRootViewController:presentedViewController];
        } else {
            return rootViewController;
        }
    
    }

    值得注意的是

      • [[[UIApplication sharedApplication] keyWindow] rootViewController]有时为nil 比如当页面有菊花在转的时候,这个rootViewController就为nil;
      • 所以使用[[[[UIApplication sharedApplication] delegate] window] rootViewController]
        或者[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController]

      • presentedViewController 和presentingViewController
        当A弹出B
        A.presentedViewController=B
        B.presentingViewController=A




  • 相关阅读:
    痞子衡嵌入式:i.MXRT1010, 1170型号上不一样的SNVS GPR寄存器读写控制设计
    痞子衡嵌入式:嵌入式Cortex-M裸机环境下临界区保护的三种实现
    《痞子衡嵌入式半月刊》 第 36 期
    痞子衡嵌入式:嵌入式MCU中标准的三重中断控制设计
    痞子衡嵌入式:了解i.MXRTxxx系列ROM中灵活的串行NOR Flash启动硬复位引脚选择
    痞子衡嵌入式:串行NOR Flash的页编程模式对于量产效率的影响
    《痞子衡嵌入式半月刊》 第 35 期
    痞子衡嵌入式:对比i.MXRT与LPC在RTC外设GPREG寄存器使用上的异同
    Springboot 配置文件、隐私数据脱敏的最佳实践(原理+源码)
    干掉 Postman?测试接口直接生成API文档,这个工具贼好用
  • 原文地址:https://www.cnblogs.com/daxueshan/p/6372007.html
Copyright © 2011-2022 走看看