当前最上层的视图控制器vc 和 当前最上层的导航控制器nav

时间:2023-03-08 20:29:02
当前最上层的视图控制器vc 和 当前最上层的导航控制器nav
在处理 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