首页 > 编程技术 > iOS

iOS如何获取最顶层ViewController详解

发布时间:2020-12-8 11:31

1 获取当前屏幕显示的 Viewcontroller

//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
 ///下文中有分析
 UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
 UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
 return currentVC;
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
 UIViewController *currentVC;
 if ([rootVC presentedViewController]) {
  // 视图是被presented出来的
  rootVC = [rootVC presentedViewController];
 }

 if ([rootVC isKindOfClass:[UITabBarController class]]) {
  // 根视图为UITabBarController
  currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]]; 
 } else if ([rootVC isKindOfClass:[UINavigationController class]]){
  // 根视图为UINavigationController
  currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]]; 
 } else {
  // 根视图为非导航类 
  currentVC = rootVC;
 }
 
 return currentVC;
}

2 分析

2.1 UIApplication 的简析

UIApplication的核心作用是提供了iOS程序运行期间的控制和协作工作,每一个程序在运行期必须有且仅有一个UIApplication(或则其子类)的一个实例,在程序启动运行时,会在 main 函数中创建一个 UIApplication的单例实例,在代码中可以通过调用[UIApplication sharedApplication]来得到这个单例实例的指针。

2.2  KeyWindow  的简析

在简析 KeyWindow 前我们先来看一看 UIWindow 的概念

UIWindow 是 UIView 的子类,其在 UIView 添加了一些视图层级,管理视图,转发 UIEvent 对象的属性和 Method 等等

在上述实例中,我们通过  [UIApplication sharedApplication] 来获取的 UIApplication 的单例实例对象,然后通过实例对象的 keyWindow再获取到当前活跃的window(或者说是当前显示的主窗口).

KeyWindow 即指在IOS开发中活跃窗口,即能接到键盘和非触摸事件的一个窗口,一次只能有一个KeyWindow,在IOS 开发中,我们可以通过设置UIWindowLevel的数值来设置最前端的窗口为哪个,Level数值越高的窗口越靠前,如果两个窗口的Level等级相同,则我们可以通过makeKeyAndVisible来显示KeyWindow

(void)makeKeyWindow;//让当前UIWindow变成keyWindow(主窗口)
(void)makeKeyAndVisible;//让当前UIWindow变成keyWindow,并显示出来
[UIApplication sharedApplication].windows //获取当前应用的所有的UIWindow
[UIApplication sharedApplication].keyWindow //获取当前应用的主窗口
view.window ///获得某个UIView所在的UIWindow

makeKeyAndVisible 与 makeKeyWindow 

becomeKeyWindow 与 resignKeyWindow

2.3 rootViewController属性

顾名思义:当前窗口的根视图

目前只有UIWindow有rootViewController这个属性,不要跟UINavigationController里面的根视图概念混淆。
UINavigationController其实并没有 rootViewController这个属性!也就没有自带的setter方法。要设置其根视图只能通过如下方法

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController; 

获取 uiwindow的根视图

方式一

AppDelegate *app =(AppDelegate *) [UIApplication sharedApplication].delegate;
UIViewController *rootViewController1 = appdelegate.window.rootViewController;

方式二

UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *rootViewController2 = window.rootViewController;

需要注意的是:

在方式二中,UIAlertController、UIAlertView、UIActionSheet弹出后,上述这些View 出现生成了一个新的window,加在了界面上面,所以keyWindow就会变成UIAlertControllerShimPresenterWindow这个类

2.4 PresentedViewController 简析

在 ios 开发中,一般页面的组成有 NavigationController  或者 其他的 UiViewController、UITabViewController 等等,

presentedViewController 与  presentingViewController

案例说明 A.presentedViewController A控制器跳转到B控制器;B.presentingViewController 就是返回到A控制器。

总结

到此这篇关于iOS如何获取最顶层ViewController的文章就介绍到这了,更多相关iOS获取最顶层ViewController内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: