Attempt to present UIAlertController on UIViewController whose view is not in the window hierarchy

吃可爱长大的小学妹 提交于 2019-12-19 08:03:12

问题


I'm trying to check the UIAlertController in iOS 9 for my sample application and while run it then I had found warning in console. I'm using Xcode 7 and Objective C.

Please find the below warning message in console.

Warning: Attempt to present < UIAlertController: 0x7fb1bb5be040 > on < ViewController: 0x7fb1bb5aef30 > whose view is not in the window hierarchy!

Please find the below code for more information.

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                                               message:@"This is an alert."
                                                        preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

回答1:


I guess, you are trying to present the alert on view did load. You get the error:

Warning: Attempt to present < UIAlertController: 0x7fb1bb5be040 > on < ViewController: 0x7fb1bb5aef30 > whose view is not in the window hierarchy!

because, in view did load the views are not yet available to display to the user. Hence you can't present the alert. Move the code into viewDidAppear



来源:https://stackoverflow.com/questions/33016497/attempt-to-present-uialertcontroller-on-uiviewcontroller-whose-view-is-not-in-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!