iOS开发-获取rootViewController的正确方式

获取工程中window上面的RootViewController keyWindow是,在windows数组中最近时间调用makeKeyAndVisible方法的属性。windows

方法一:建议使用

AppDelegate *app =(AppDelegate *) [UIApplication sharedApplication].delegate;
UIViewController *rootViewController1 = appdelegate.window.rootViewController;
复制代码
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if ([appdelegate.window.rootViewController isKindOfClass:["xxxrootVC" class]] == YES) {
        // 这样更保险
    }
复制代码

方法二:不建议使用

UIAlertController或UIAlertView或UIActionSheet弹出后,keyWindow就会变成UIAlertControllerShimPresenterWindow这个类数组

UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *rootViewController2 = window.rootViewController;
复制代码

alertView的出现是由于,生成了一个新的window,加在了界面上面。 这个时候获取到的keyWindow就是UIAlertControllerShimPresenterWindow。bash

相关文章
相关标签/搜索