iOS获取最顶层ViewController

5,699 阅读3分钟

1.获取当前屏幕显示的 Viewcontroller 案例源码 2.UIApplication 的简析 3.KeyWindow 的简析 4.rootViewController 的简析 5.PresentedViewController 的简析

在这里插入图片描述

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

  • makeKeyWindow: 只做了一件事就是使当前窗口成为主要窗口。 当前窗口不一定显示出来

  • makeKeyAndVisible : 做了两件事使当前窗口成为主要窗口并显示当前窗口。如果只想显示的话,我们还可以通过设置其属性hidden为 NO 即可。=

becomeKeyWindow 与 resignKeyWindow

  • becomeKeyWindow: 程序自动调用,用来通知其他窗口 当前容器已被设置为主窗口。 我们不要主动调用此方法, 此方法是系统自动调用的来发通知的,此方法的默认实现不执行任何操作,但子类可以覆盖它并使用它来执行与成为关键窗口相关的任务。

  • resignKeyWindow: 类似becomeKeyWindow, 其是调用以通知窗口它将要注销掉主键窗口的身份.同样的切勿直接调用此方法。

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 等等,

  • 在有NavigationController导航栏的话,使用[self.navigationColler pushViewController:animated:] 进入到下一个视图 ,使用[self.navigationController popViewControllerAnimated:] 返回到上一视图。

  • 在没有NavigationController导航栏的时候,使用[self presentViewController:animated:completion:] 进入到下一个视图,使用 [self dismissViewControllerAnimated:completion:];返回到上一个视图中。

presentedViewController 与 presentingViewController

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

在这里插入图片描述