我知道這已被問及一百萬次,但我無法在許多問題中找到合適的答案,已經檢查過。警告:嘗試在<UINavigationontroller>上呈現其視圖不在窗口層次結構中的<CustomViewController>
我有一個自定義視圖控制器,我試圖顯示視圖控制器時,用戶點擊一個按鈕(所以沒有「臭名昭着的viewDidLoad
問題」在這裏)。
這裏是我的代碼運行,當用戶點擊該按鈕:(我有NIB的視圖控制器,我有一個導航控制器)
ICLoginViewController *loginViewController = [[ICLoginViewController alloc] initWithNibName:@"ICLoginViewController" bundle:[NSBundle mainBundle]];
//assuming we have a navigation controller.
UINavigationController *navigationController= (UINavigationController*)[[UIApplication sharedApplication] keyWindow].rootViewController;
[navigationController.topViewController presentViewController:loginViewController animated:YES completion:nil];
,我發現了Warning: Attempt to present <ICLoginViewController: 0xa08a810> on <UINavigationController: 0xa45de70> whose view is not in the window hierarchy!
錯誤,當我嘗試呈現視圖控制器。屏幕上沒有任何反應。如果我多次點擊,我會得到同樣的錯誤,但仍然沒有任何反應。我已經設置了一個斷點並驗證了navigationController
和navigationController.topViewController
不是nil
。我'使用故事板(如果有幫助),但不是我試圖顯示的自定義視圖控制器。 (從長遠來看,我想讓它成爲一個獨立於應用程序的庫,所以我沒有引用任何特定於應用程序的模塊)爲什麼會出現此錯誤?
是否有一個特定的原因,爲什麼不使用self.navigationController? – Adis
是的,我喜歡。我將這段代碼放在一個單獨的文件中,這個文件將獨立於應用程序,並且這裏的'self'只是一個NSOBject派生的自定義類,而不是視圖控制器。 –