2015-10-08 34 views
4

我试图在iOS 9中检查我的示例应用程序的UIAlertController,并在运行它时发现了控制台中的警告。我正在使用Xcode 7和Objective C.试图在其视图不在窗口层次结构中的UIViewController上呈现UIAlertController

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

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

请找到下面的代码以获取更多信息。

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]; 
+0

你在哪里调用这段代码? –

+0

你在viewDidload上调用了这个吗? –

+0

@ Mr.T:是的,我打电话给viewDidLoad。 –

回答

13

我想,你正试图提出警告视图做负载。你得到的错误:

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

因为,在视图中确实加载视图尚不可显示给用户。因此你不能提出警报。将代码移动到viewDidAppear

+3

这应该得到更多upvotes。使用'dispatch_async(dispatch_get_main_queue(),^ {...});'的普遍接受的解决方案是一种破解。 –

相关问题