解决 警告Presenting view controllers on detached view controllers is discouraged <HtmlViewController: 0x7f939ea02ce0>

模态跳转的时候有时会会出现这个警告:

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;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容