尝试在UIViewController上呈现UIAlertController,其视图不在窗口层次结构中

Ram*_*rma 5 objective-c hierarchy uiviewcontroller ios9

我正在尝试检查iOS 9中的UIAlertController以获取我的示例应用程序,然后运行它然后我在控制台中发现了警告.我正在使用Xcode 7和Objective C.

请在控制台中找到以下警告消息.

警告:尝试在<ViewController:0x7fb1bb5aef30>上显示<UIAlertController:0x7fb1bb5be040>,其视图不在窗口层次结构中!

请查看以下代码以获取更多信息.

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];
Run Code Online (Sandbox Code Playgroud)

Tej*_*uri 14

我想,你试图在视图上显示加载的警报.你得到错误:

警告:尝试在<ViewController:0x7fb1bb5aef30>上显示<UIAlertController:0x7fb1bb5be040>,其视图不在窗口层次结构中!

因为,在视图中确实加载了尚未显示给用户的视图.因此,您无法显示警报.将代码移入viewDidAppear

  • 这应该得到更多的赞成.使用`dispatch_async(dispatch_get_main_queue(),^ {...});`的普遍接受的解决方案是一个黑客. (4认同)