![](/img/trans.png)
[英]UIViewControllerHierarchyInconsistency when trying to present a modal view controller
[英]UIViewControllerHierarchyInconsistency when presenting view controller
我有這樣的View Controller結構: UINavigationController(root view controller)->UIViewController
。 在我的UIViewController
我有帶有動態單元格的UITableView
。 在每個單元格中都有一個“共享”按鈕,用於顯示全屏UIWindow
。 此UIWindow
包含帶有社交網絡按鈕的UIView
,每個按鈕必須顯示共享對話框,並且僅對於與社交網絡框架或庫一起使用的按鈕一切正常,但是我有一個按鈕必須呈現自定義共享對話框。 當我按它時,我的應用程序崩潰並出現以下錯誤: *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'adding a root view controller <REComposeViewController: 0x7f9e8b416260> as a child of view controller:<AVMNavCon: 0x7f9e887540f0>'
這就是我嘗試顯示對話框的方式:
-(void)shareWithLinkedIn:(AVMSocNetButton*)sender{
[self closeWhiteView]; // close UIWindow with social network buttons
REComposeViewController *composeViewController = [[REComposeViewController alloc] init]; // define and initialize custom share dialog
composeViewController.title = @"Social";
composeViewController.hasAttachment = YES;
composeViewController.attachmentImage = sender.shareImage;
composeViewController.text = [NSString stringWithFormat:@"Share text"];
testWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 74, SCREEN_WIDTH-10, SCREEN_HEIGHT/2)];
testWindow.windowLevel = UIWindowLevelStatusBar;
testWindow.hidden = NO;
testWindow.rootViewController = composeViewController;
[composeViewController presentFromRootViewController];
[self performSelector:@selector(showShareWindow) withObject:nil afterDelay:1];
}
每個人都可以在這里看到另一個UIWindow
(testWindow)。 這是因為如果我不使用UIWindow
顯示對話框時,我的UIViewController
將消失,並且我會在黑色背景上看到共享對話框。 然后,如果我注釋行testWindow.rootViewController = composeViewController;
我將根據需要看到共享對話框,但沒有任何交互(我無法在屏幕上的任何地方觸摸按鈕)如何顯示對話框並避免出現此錯誤?
編輯:
完整的層次結構是: UINavigationController
> UIViewController
> UITableView
> UITableViewCell
> UIButton
->(調用“ WhiteView” UIWindow
)-> UIWindow
> UIButton
->(調用shareWithLinkedIn
方法)
您不應嘗試將窗口的根視圖控制器用作模式。 如果要使用窗口實現此過渡,請使用背景清晰的空UIViewController
作為rootViewController
,然后將您的composeViewController
為模態。
您可以在不使用Windows的情況下完成您想做的事情。 例如在iOS 8,使用UIModalPresentationOverFullScreen
作為modalPresentationStyle
您composeViewController
和簡單地呈現它作為當前的控制器模態。 同樣,您需要一個透明的容器視圖來執行此操作,但是它比引入另一個窗口更干凈。 在iOS 7上,您需要使用UIModalPresentationCustom
(默認情況下似乎具有與UIModalPresentationOverFullScreen
相同的效果)
您要做的就是將VC呈現在根視圖控制器之上:
- (void)shareWithLinkedIn:(AVMSocNetButton*)sender{
REComposeViewController *composeViewController = [[REComposeViewController alloc] init]; // define and initialize custom share dialog
composeViewController.title = @"Social";
composeViewController.hasAttachment = YES;
composeViewController.attachmentImage = sender.shareImage;
composeViewController.text = [NSString stringWithFormat:@"Share text"];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:composeViewController animated:YES completion:nil];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.