呈现视图控制器时的 UIViewControllerHierarchyInconsistency

UIViewControllerHierarchyInconsistency when presenting view controller

我有这样的 View Controller 结构:UINavigationController(root view controller)->UIViewController。在我的 UIViewController 中,我有 UITableView 动态单元格。在每个单元格中都有 "share" 按钮显示全屏 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方法)

你想要做的就是在根视图控制器上展示你的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];
}

您不应尝试将 window 的根视图控制器用作模式。如果您想使用 window 实现此转换,则使用具有清晰背景的空 UIViewController 作为 rootViewController,然后将您的 composeViewController 作为模态呈现。

您可以在不使用 windows 的情况下完成您想要做的事情。例如,在 iOS 8 上,将 UIModalPresentationOverFullScreen 用作 composeViewControllermodalPresentationStyle,并将其简单地呈现为当前控制器的模态。同样,您需要一个透明的容器视图来执行此操作,但它比引入另一个 window 更简洁。在 iOS 7 上,您需要使用 UIModalPresentationCustom(默认情况下似乎与 UIModalPresentationOverFullScreen 具有相同的效果)