2016-08-25 89 views
1

當我嘗試從我的應用程序中顯示UIAlertController時,應用程序終止,並出現異常UIViewControllerHierarchyInconsistencyUIViewControllerHierarchyInconsistency當顯示UIAlertController時出現異常

視圖控制器與故事板

From Storyboard editor

創建和(嘗試)顯示警報這樣創建:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; 
UIAlertAction *yesButton = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
...  
}]; 
[alert addAction:yesButton]; 

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

然而,在執行過程中我得到這個在輸出:

2016-08-25 09:46:07.536 TrackYou [10554:3165715] *** Terminati NG應用程序由於未捕獲的異常 'UIViewControllerHierarchyInconsistency',理由是: '子視圖控制器:< UICompatibilityInputViewController:0x13f5afd80>應該有父視圖控制器:<的ViewController:0x13f549360>,但要求家長爲:< UIInputWindowController:0x140043c00>'

從相同的ViewController我使用presentViewController來呈現一個自定義ViewController,它工作正常。

SettingsViewController *controller = [SettingsViewController new]; 
[self presentViewController:controller animated:YES completion:nil]; 

任何想法是什麼原因造成的問題?

+0

可能欺騙http://stackoverflow.com/questions/24029113的/ error-when-adding-input-view-to-textfield-ios-8 .. – vaibhav

回答

0

我也堅持這個問題,並找到解決辦法是,你應該找到頂部視圖控制器,並在頂部視圖控制器上顯示警報。

查找頂視圖控制器:

-(UIViewController *)getTopViewController{ 
      UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; 

     while (topController.presentedViewController) { 
      topController = topController.presentedViewController; 
     } 
     if (![topController isKindOfClass:[NSNull class]]) { 
      return topController; 
    } 

並呈現在頂部視圖控制器警報:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; 
UIAlertAction *yesButton = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
...  
}]; 
[alert addAction:yesButton]; 

[[self getTopViewController] presentViewController:alert animated:YES completion:nil]; 
+0

感謝您的回覆。不幸的是,它並沒有解決我的問題。相同的錯誤:( – Smorka

相關問題