详解iOS开发获取当前控制器的正取方式

2020-01-21 07:17:19王旭

背景

在开发过程中,经常需要获取当前 window, rootViewController, 以及当前 ViewController 的需求. 如果 .m 实现不是在当前视图情况下, 我们需要快速的获取到当前控制器, 这种情况就需要先做好一层封装,我一般是通过 UIViewController 写的一个 Category 来实现, 实现起来也非常简单, 只需要我们对 控制器几个方法掌握便可。

获取根控制器


+ (UIViewController *)jsd_getRootViewController{

 UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
 NSAssert(window, @"The window is empty");
 return window.rootViewController;
}

这里很简单, 通过单例获取到当前 UIApplication 的 delegate 在通过 window 即可轻松拿到 rootViewController。

获取当前页面控制器


+ (UIViewController *)jsd_getCurrentViewController{

 UIViewController* currentViewController = [self jsd_getRootViewController];
 BOOL runLoopFind = YES;
 while (runLoopFind) {
  if (currentViewController.presentedViewController) {

   currentViewController = currentViewController.presentedViewController;
  } else if ([currentViewController isKindOfClass:[UINavigationController class]]) {

   UINavigationController* navigationController = (UINavigationController* )currentViewController;
   currentViewController = [navigationController.childViewControllers lastObject];

  } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {

   UITabBarController* tabBarController = (UITabBarController* )currentViewController;
   currentViewController = tabBarController.selectedViewController;
  } else {

   NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
   if (childViewControllerCount > 0) {

    currentViewController = currentViewController.childViewControllers.lastObject;

    return currentViewController;
   } else {

    return currentViewController;
   }
  }

 }
 return currentViewController;
}

这里讲一下实现思路, 我们想要与控制器无耦合的情况下, 想要直接获取到当前控制器, 基本都是通过 rootViewController 来查找的, 通过上面的方法拿到 rootViewControoler 之后, 我们先看 presentedViewController, 因为控制器呈现出来的方式有 push 与 present, 我们先查看它是否是 present 出来的, 如果是则通过此属性能找到 present 出来的当前控制器, 然后在检查是否属于 UINavigationControler 或 UITabBarController ,如果是则通过查找其子控制器里面最顶层或者其正在选择的控制器。