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