解决警告Presenting view controllers on detached view controllers is discouraged 以及引申

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

Presenting view controllers on detached view controllers is discouraged .


原因是present出来的模态窗口,禁止再使用present 来弹出其它的子窗口

只要把self直接模态跳转页面改成从根控制器跳转即可

例如:

 PicDetailViewController *detailVC = [[PicDetailViewController alloc] init];

[self presentViewController:detailVC animated:YES completion:nil];

改成

  AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

 [delegate.window.rootViewController presentViewController:detailVC animated:YES completion:nil];

即可

引申一下如何找根控制器:

方法一:就是通过上面的方法,先找到appdelegate然后获取window属性

  LeftSlideViewController *rootVc = (LeftSlideViewController*)[(AppDelegate *)[UIApplication sharedApplication].delegate window].rootViewController;

这种方法除了找根控制器还能找appdelegate类里的定义的任何属性,如:解决警告Presenting view controllers on detached view controllers is discouraged 以及引申_第1张图片

方法二:直接通过keyWindow来找根控制器,更直接

  LeftSlideViewController *rootVc = (LeftSlideViewController*)[UIApplication sharedApplication].keyWindow.rootViewController;



你可能感兴趣的:(iOS开发)