rootViewController获取真的如你想的那么简单吗
来源:互联网 发布:淘宝买鹿王羊绒衫 编辑:程序博客网 时间:2024/06/07 21:32
本篇转载自: http://www.qingpingshan.com/rjbc/ios/181557.html
你一般怎么获取工程中window上面的RootViewController
第一种方法:
UIWindow *window = [UIApplication sharedApplication].keyWindow; UIViewController *rootViewController = window.rootViewController;
第二种方法:
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; UIViewController *rootViewController1 = appdelegate.window.rootViewController;
这两种写法,在平常的时候是没有区别的,但是这两种写法在有的时候就不一样了。
keyWindow这个属性是什么意思?
Paste_Image.png
个人理解的意思是,在windows数组中,最近时间调用makeKeyAndVisible方法的属性。
当然可以参考关于UIAlertView显示的相关内容:
http://www.jianshu.com/p/7ac398ef4532
http://stackoverflow.com/questions/5968703/how-to-find-root-uiviewcontrolleralertView的出现是因为,生成了一个新的window,加在了界面上面。这个时候获取到的keyWindow就是UIAlertControllerShimPresenterWindow。可以通过如下代码实验:
Appdelegate中的代码,设置RootViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor]; ViewController *view = [[ViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:view]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; return YES;}
ViewController中的代码
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeSystem]; tempBtn.frame = CGRectMake(100, 100, 100, 100); tempBtn.backgroundColor = [UIColor cyanColor]; [tempBtn setTitle:@"测试1" forState:UIControlStateNormal]; [tempBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:tempBtn];}
- (void)clickBtn:(UIButton *)sender{// 创建一个测试的alertViewUIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"测试" message:@"测试" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil]; [alterView show]; UIWindow *window1 = [UIApplication sharedApplication].keyWindow; AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; UIWindow *window2 = appdelegate.window; NSLog(@"\n\nwindow1 = %@ \n\nwindow2 = %@ \n\nwindow1.rootViewController = %@ \n\nwindow2.rootViewController = %@",window1,window2,window1.rootViewController,window2.rootViewController);}
打印结果:
window1 = <_UIAlertControllerShimPresenterWindow: 0x7fb4ab720c80; frame = (0 0; 375 667); opaque = NO; gestureRecognizers = <NSArray: 0x7fb4ab72fac0>; layer = <UIWindowLayer: 0x7fb4ab7223f0>> window2 = <UIWindow: 0x7fb4ab720650; frame = (0 0; 375 667); gestureRecognizers = <NSArray: 0x7fb4ab722310>; layer = <UIWindowLayer: 0x7fb4ab720a70>> window1.rootViewController = <UIApplicationRotationFollowingController: 0x7fb4ab9282e0> window2.rootViewController = <UINavigationController: 0x7fb4ae820800>
结果明显不一样,其实我们一般情况下想获取的rootViewController是第二种,希望我们获取到在appdelegate中设置的appdelaget.window.rootViewController。
所以了建议获取rootViewController的时候还是采用
第二种方法:
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; UIViewController *rootViewController1 = appdelegate.window.rootViewController;
其实,和alertView类似的,UIActionSheet也是这样的。
一般人说无所谓,但是如果在AlertView弹出的时候去获取RootViewController,并且对你认为获取正确的RootViewController做相关的操作,你会死的很惨。
还有就是建议:即时通过第二种方法获取到了RootViewController,在使用之前建议再判断一下获取到的类是不是就是自己想要的类型,更保险一些。
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; if ([appdelegate.window.rootViewController isKindOfClass:["想要获取到的rootVC" class]] == YES) { // 为所欲为 }
标题写的有点大哈,为了吸引眼球的。
以上是我自己根据相关的运行效果总结的,如果有哪位大神觉得有地方描述的不准确,欢迎指正哈,在下感激不尽!!!
- rootViewController 获取真的如你想的那么简单吗?
- rootViewController获取真的如你想的那么简单吗
- 二分查找真的有你想象中那么简单吗?
- 二分查找真的有你想象中那么简单吗?
- 单例没你想的那么简单
- Python lambda没有你想的那么简单
- 有些事情,真的没有你想的那么难
- rootViewController获取遇到的坑
- 真正的Hybrid APP没你想的那么简单
- 并行编程,绝不是你想的那么简单
- 左旋转字符串 (可能不是你想的那么简单)
- 舆情中的热词分析,没你想的那么简单
- LeetCode:String to Integer (atoi)没你想的那么简单
- 原生JS|数据类型检测,并没你想象的那么简单
- NSDate 8小时问题-没你想的那么简单!
- 网页制作真的那么简单?
- 网页制作真的那么简单?
- 获取uiview的uiviewcontroller和rootviewcontroller
- 邻接链表与图及其相关操作--Java
- 微服务指南走北(四):你不愿意做微服务架构的十个理由
- php中类之间继承关系
- c++实现解析文件路径
- 动态规划总结
- rootViewController获取真的如你想的那么简单吗
- Tomcat 8005、8009和8080端口
- Leetcode——Array 2
- SpringMVC学习系列(8) 之 国际化
- C 编译错误 warning : no newline at end of file 解决
- 第五章 5-3 画刷
- Coordinationlayout解析之二
- vb.net 教程 3-2 窗体编程之窗体 2
- OkHttp Wiki翻译(三)连接