解雇QLPreviewController崩溃:仍然需要自动布局

时间:2013-04-06 02:23:02

标签: ios objective-c

我正在使用SplitViewController的iPad应用。在我的DetailViewController中,有一个按钮,用于显示QLPreviewController并显示文档。到目前为止一切正常,但是,当我使用左上角的Done按钮关闭预览控制器时,应用程序会抛出异常并出现以下错误:

*** Assertion failure in -[UIView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2380.17/UIView.m:5781
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after sending -viewDidLayoutSubviews to the view controller. DetailViewController's implementation needs to send -layoutSubviews to the view to invoke auto layout.'
*** First throw call stack: (...)
libc++abi.dylib: terminate called throwing an exception

以下是我实现QLPreviewController及其委托方法的演示文稿的方法:

- (IBAction)previewButtonPressed:(id)sender
{
    QLPreviewController *ql = [[QLPreviewController alloc] init];
    ql.dataSource = self;
    ql.delegate = self;
    ql.currentPreviewItemIndex = 0;
    [self presentViewController:ql animated:YES completion:NULL];
}

- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    return [NSURL fileURLWithPath:self.documentFilePath];
}

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
    return 1;
}

viewDidLayoutSubviews

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self.scrollView setContentSize:self.gridView.frame.size]; // if not called, the contentSize of UIScrollView is wrong.
}

DetailViewController包含UIScrollView,它有一个自定义UIView(gridView)作为子视图,gridView有很多子视图。

在测试时,我注释掉了从viewDidLayoutSubviews设置内容大小的代码行,通过这样做,我可以成功地关闭预览控制器。但是,这仅在我将gridView作为子视图添加到scrollView之前有效。将其添加为子视图后,它会在解雇后再次崩溃。这次我得到EXC_BREAKPOINT错误并且没有登录控制台。调试器的输出如下:

CoreFoundation`CFHash:
0x4597740:  pushl  %ebp
0x4597741:  movl   %esp, %ebp
0x4597743:  pushl  %edi
0x4597744:  pushl  %esi
0x4597745:  subl   $16, %esp
0x4597748:  calll  0x459774d                 ; CFHash + 13
0x459774d:  popl   %edi
0x459774e:  movl   8(%ebp), %esi
0x4597751:  testl  %esi, %esi
0x4597753:  jne    0x459776b                 ; CFHash + 43
0x4597755:  int3   
0x4597756:  calll  0x46eca00                 ; symbol stub for: getpid  <-  EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
0x459775b:  movl   %eax, (%esp)
0x459775e:  movl   $9, 4(%esp)
0x4597766:  calll  0x46eca4e                 ; symbol stub for: kill
...

请注意,该应用针对iOS 6并使用自动布局。

我想知道是否有人可以帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:6)

viewDidLayoutSubviews实施中,执行错误消息告诉您的操作:致电[self.view layoutSubviews]。看看是否有帮助。

编辑:可能我在前一段中的建议应该是拨打layoutIfNeeded而不是layoutSubviews

要尝试的另一件事,如果不起作用:将整个内容大小设置事件移至viewWillLayoutSubviews,而不是在viewDidLayoutSubviews中执行。