2015-11-07 74 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]

相關問題