IOS 開發中 Whose view is not in the window hierarchy 錯誤的解決辦法

我是在做一個藍牙項目遇到這種問題的,需求是藍牙連接后按設備按鈕調用相機并且控制相機拍照,因此我選擇使用自定義相機,設備發送的參數都是在一個cordova插件中實現的,它的父類是NSObject類型的。當我實現跳轉到自定義的相機控制器里面時,一直出現whose view is not in the window hierarchy 這個錯誤,最后,我把在 viewDidLoad 里面的方法轉移到 viewDidAppear 方法里面,然后就解決了。
該錯誤簡單的說,是由于 "ViewController" 還沒有被加載,就調用該 ViewController 或者 ViewController 內的方法時,就會報這個錯誤。在不同地方調用 ViewController,解決的方法也不太一樣。

  1. 在 一個 ViewController 里面調用另外一個 ViewController 是出現這個錯誤:

該錯誤一般是由于在 viewDidLoad 里面調用引起的,解決辦法是轉移到 viewDidAppear 方法里面

  1. 在 AppDelegate.m 中調用遇到這個錯誤

解決辦法1:

UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topRootViewController.presentedViewController)
 {
    topRootViewController = topRootViewController.presentedViewController;
 }
 
//[topRootViewController presentViewController:yourController animated:YES completion:nil];
//or
[topRootViewController myMethod];
 

解決辦法2:

UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
   LoginViewController* loginViewController = [mainstoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
   [self.window makeKeyAndVisible];
//[LoginViewController presentViewController:yourController animated:YES completion:nil];
//or
[LoginViewController myMethod];

參考地址:http://stackoverflow.com/questions/11862883/whose-view-is-not-in-the-window-hierarchy

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容