模态跳转的时候有时会会出现这个警告:
Presenting view controllers on detached view controllers is discouraged 。
这种现象的原因是:present出来的模态窗口,禁止再使用present 来弹出其它的子窗口
只要把self直接模态跳转页面改成从根控制器跳转即可。
即把:
UIAlertController* alertC = [UIAlertController alertControllerWithTitle:@"系统公告" message:@"目录存在" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertC addAction:alertAction];
[self presentViewController:alertC animated:YES completion:nil];
改为:
UIAlertController* alertC = [UIAlertController alertControllerWithTitle:@"系统公告" message:@"目录存在" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertC addAction:alertAction];
AppDelegate* delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window.rootViewController presentViewController:alertC animated:YES completion:nil];即可。
如何找根控制器:
方法一:
HtmlViewController *rootVc = (HtmlViewController*)[(AppDelegate *)[UIApplication sharedApplication].delegate window].rootViewController;
方法二:
直接通过keyWindow来找根控制器,更直接
HtmlViewController *rootVc = (HtmlViewController*)[UIApplication sharedApplication].keyWindow.rootViewController;