【问题标题】:UIViewControllerHierarchyInconsistency when presenting view controller呈现视图控制器时的 UIViewControllerHierarchyInconsistency
【发布时间】:2015-02-09 05:13:35
【问题描述】:

我有这样的视图控制器结构: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->@987654340 @->(调用shareWithLinkedIn方法)

【问题讨论】:

  • showShareWindow 是做什么的?而closeWhiteView实际上关闭了testWindow?
  • @gabbler showShareWindow 执行 [testWindow makeKeyAndVisible]closeWhiteView 关闭 UIWindow 其中包含调用 -(void)shareWithLinkedIn:(AVMSocNetButton*)sender 方法的按钮。请参阅我的编辑以获取解释
  • 你是如何关闭WhiteView并关闭窗口的?
  • @gabbler -(void)closeWhiteView{ [socialWindow resignKeyWindow]; socialWindow.hidden = YES; socialWindow = nil; } 效果很好。
  • 我认为testWindow.hidden = YES; 会按预期工作,这是因为 testWindow 在前面并阻止了按钮的触摸事件,但是 textView 已经成为第一响应者,所以它是可编辑的.

标签: ios objective-c uitableview uinavigationcontroller slcomposeviewcontroller


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 它不起作用。它使屏幕变黑,尽管NSLog 显示 composeViewController 存在的消息:composeViewController <REComposeViewController: 0x7f8794202a00>
    • @vendettacore,REComposeViewController 做什么?
    • github.com/romaonthego/REComposeViewController 这里是完整的解释。所以我尝试执行以下操作:[[[[UIApplication sharedApplication] keyWindow] rootViewController] addChildViewController:composeViewController]; [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:composeViewController animated:YES completion:nil]; 它向我展示了我想要的东西,但它杀死了以前的 UIViewController
    • @vendettacore,为什么不使用SLComposeServiceViewController
    • @vendettacore,试试[composeViewController presentFromRootViewController]; 而不是[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:composeViewController animated:YES completion:nil];
    猜你喜欢
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2017-04-10
    相关资源
    最近更新 更多