欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-07-04 20:20:48
背景 在开发过程中,经常需要获取当前 window, rootviewcontroller, 以及当前 viewcontroller 的需求. 如果 .m 实现不是在...

背景

在开发过程中,经常需要获取当前 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 ,如果是则通过查找其子控制器里面最顶层或者其正在选择的控制器。
最后在判断当前控制器是否有子控制器的情况, 如果有则取其子控制器最顶层, 否则当前控制器就是其本身。

这里主要是查找当前 应用程序基于 uitabbarcontroller 和 uinavigationcontroler 下管理的视图控制器, 如果还有其他控制器则需要添加 if 条件来进行判断。

presentedviewcontroller

apple 文档 presentedviewcontrolle

通过此方法可以查找到通过 presented 模态方式(显示与隐士) 方式推出的当前控制器。

例如: aviewcontroller --> bviewcontroller 通过模态方式推出.

则使用 aviewcontroller.presentedviewcontroller 能获取到 bviewcontroller。

presentingviewcontroller

apple 文档

通过此方法可以查找到通过 presented 模态方式(显示与隐士) 方式推出当前控制器的上层控制器。

例如: aviewcontroller --> bviewcontroller 通过模态方式推出.

则使用 bviewcontroller.presentingviewcontroller 能获取到 aviewcontroller。

modalviewcontroller

查看文档发现此方法已在 ios 6 后被弃用, 官方推荐直接使用 presentedviewcontroller 替代即可.

ps:通过view获取view所在的控制器

+(uiviewcontroller*)getviewcontrollerview:(uiview*)view{
 
 uiviewcontroller *vc = nil;
 for (uiview *temp = view; temp;temp = temp.superview) {
  if ([temp.nextresponder iskindofclass:[uiviewcontroller class]]) {
   vc = (uiviewcontroller*)temp.nextresponder;
   break;
  }
 }
 return vc;
}

通过递归方法遍历当前view的所有子试图

+(void)getmysubviewswithviews:(uiview *)view{
 nsarray *arrayviews = view.subviews;
 for (uiview * obj in arrayviews) {
  if ([obj iskindofclass:[myview class]]) {
   nslog(@"找到了 %@",myview);
  }
  [self getmysubviewswithviews:obj];
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。