2015-11-07 75 views
0

我这里有这样的代码是想拍摄出警告:的Objective-C,其观点是不是在窗口层次! UIAlertController

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message" message:@"Device is not valid." preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; 
[alertController addAction:ok]; 

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

但我警告不会出现,我得到这个错误在我的控制台日志:

Attempt to present <UIAlertController: 0x134e5d350> on <ViewController: 0x135008c00> whose view is not in the window hierarchy! 

我是什么做错了?我试图谷歌的解决方案,但我不明白他们中的任何一个。请帮忙。

+2

什么是“自我”?你是一个视图控制器是视图栈上呈现'UIAlertController'? – JAL

+1

您是否[尝试搜索错误](http://stackoverflow.com/search?q=whose+view+is+not+in+the+window+hierarchy+uialertcontroller)? – rmaddy

+0

我试图把它呈现给视图控制器 –

回答

0

我小费您正在执行(在UIViewController子类)此代码之前,你的看法被添加到窗口,也许在viewDidLoad中或viewWillAppear中。你需要等待,直到你肯定是在屏幕上,尝试viewDidAppear:

-1

我以前的某个时候遇到这个问题。我不知道你们,但我觉得你的报警控制器鉴于将在这个时候出现的观点是没有准备好,以显示您的警报控制器尝试做在viewDidApper见下面的截图。 see error in log

![成功运行] [2]

相关问题