当前最上层的视图控制器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




转载于:https://www.cnblogs.com/daxueshan/p/6372007.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值