iOS Warning: Attempt to present on whose view is not in the window hierarchy!

今天在项目中有个需求,就是在接收到通知后弹出一个UIAlertController,但是并没有弹出来,出现了下面的错误信息提示:

Warning: Attempt to present  on  whose view is not in the window hierarchy!

我原来的做法是找到当前的VC后,就present

 [vc presentViewController:alertController animated:YES completion:nil];

结果是并没有弹出提示。根据这个提示信息我在查找了一下关于这个问题的解决方法,在这篇文章中找到了一个解决的方法,于是,按照这篇文章的解决方法,我就更改了我的代码

AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
UINavigationController *navVC = (UINavigationController *)appDelegate.window.rootViewController;
UIViewController *vc = navVC.visibleViewController;
[vc presentViewController:alertController animated:YES completion:nil];

就可以弹出你想要的提示了!

你可能感兴趣的:(iOS Warning: Attempt to present on whose view is not in the window hierarchy!)