关于Presenting view controllers on
摘要: 关于形如 Presenting view controllers on detached view controllers is discouraged 的处理
使用模态跳转时,Xcode有时候会出现如下警告
Presenting view controllers on detached view controllersisdiscouraged <>
这样的警告代码,如果你认为你的层次之间没有问题(其实就是层次问题。present出来的模态窗口,禁止再使用present 来弹出其它的子窗口)只要把self直接模态跳转页面改成从根控制器跳转即可
解决方法:
把
ViewController *viewController = [[ViewController alloc] init];[selfpresentViewController:viewController animated:YEScompletion:nil];
改成
AppDelegate *delegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;ViewController *viewController = [[ViewController alloc] init];[delegate.window.rootViewController presentViewController:viewController animated:YEScompletion:nil];
即可
方法一:就是通过上面的方法,先找到appdelegate然后获取window属性
KLViewController *rootVc = (KLViewController *)[(AppDelegate *)[UIApplication sharedApplication].delegatewindow].rootViewController;
这种方法除了找根控制器还能找appdelegate类里的定义的任何属性,如:
- (void) openOrcloseTheList { AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplicationsharedApplication] delegate];if(tempAppDelegate.theListVC.closed) { [tempAppDelegate.theListVC openListView]; }else{ [tempAppDelegate.theListVC closeListView]; }}
方法二:直接通过keyWindow来找根控制器,更直接
KLViewController *rootVc = (KLViewController *)[UIApplicationsharedApplication].keyWindow.rootViewController;
补充:
YZLoginViewController *yzLoginVc = [YZLoginViewController new];//找根控制器方法//1.找到appdelegate然后获取window属性AppDelegate *delegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;[delegate.window.rootViewController presentViewController:yzLoginVc animated:YEScompletion:nil];//2.直接通过keyWindow来找根控制器YZYuQingViewController *rootVc = (YZYuQingViewController *)[UIApplicationsharedApplication].keyWindow.rootViewController;[rootVc presentViewController:yzLoginVc animated:YEScompletion:nil];
链接:https://www.jianshu.com/p/157cab691ad2