获取工程中window上面的RootViewController
来源:互联网 发布:紫砂壶开壶 知乎 编辑:程序博客网 时间:2024/05/18 12:39
你一般怎么获取工程中window上面的RootViewController
第一种方法:
UIWindow *window = [UIApplication sharedApplication].keyWindow; UIViewController *rootViewController = window.rootViewController;
第二种方法:
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; UIViewController *rootViewController1 = appdelegate.window.rootViewController;
这两种写法,在平常的时候是没有区别的,但是这两种写法在有的时候就不一样了。
keyWindow这个属性是什么意思?
个人理解的意思是,在windows数组中,最近时间调用makeKeyAndVisible方法的属性。
当然可以参考关于UIAlertView显示的相关内容:
http://www.jianshu.com/p/7ac398ef4532
http://stackoverflow.com/questions/5968703/how-to-find-root-uiviewcontroller
alertView的出现是因为,生成了一个新的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) { // 为所欲为 }
说的很有道理,今天在弹出alertView的时候就遇到了这个问题,获取的rootViewContraller不是之前认为的控制器了
链接:http://www.jianshu.com/p/ae84cd31d8f0
- 获取工程中window上面的RootViewController
- rootViewController获取遇到的坑
- 获取rootviewcontroller Whose view is not in the window hierarchy 错误的解决办法
- 获取uiview的uiviewcontroller和rootviewcontroller
- self.window.rootViewController
- UISplitViewController 只能用在最开始的 window.rootViewController
- 关于self.window.rootViewController的方法调用顺序
- 获取跟控制器rootViewController
- 在ios 下获取 rootviewcontroller 的版本不同的问题
- rootViewController 获取真的如你想的那么简单吗?
- rootViewController获取真的如你想的那么简单吗
- iOS didFinishLaunchingWithOptions方法里获取storyBoard的rootViewController
- 本地相册选择照片或相机拍照 获取当前Window最上面的 UIViewController
- [ios]使用self.view.window.rootViewController presentViewControllert跳转后的返回
- self.window.rootViewController都干了些啥事?
- 获取iframe中window对象的方法
- eclipse导入github上面的工程
- java项目,工程名的上面有个小红叉
- 在VMware中安装Ubuntu 16
- bootstrap实现垂直导航框架
- 【Android自定义View】仿Photoshop取色器ColorPicker(二)
- 【NOIP2017提高组A组模拟8.17】行程的交集
- 中介者模式(Mediator Pattern)。
- 获取工程中window上面的RootViewController
- JZOJ2017.08.12 C组
- 2017-08-17 SSM 环境搭建
- Redis主从自动切换
- singlenumber
- centos7下快速安装mysql
- mysql源码学习笔记:系统变量variables
- dubbo-服务消费端类图
- 小经验总结-maven搭建