UIDocumentInteractionController的奇怪问题

Tùn*_* Đỗ 15 iphone document ipad

我不知道这段代码有什么问题,但每当我运行应用程序时,在显示菜单后,应用程序崩溃.

NSString * path = [[NSBundle mainBundle] pathForResource:@"tung" ofType:@"doc"];

UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];

docController.delegate = self;

//[docController presentPreviewAnimated:YES];

CGRect rect = CGRectMake(0, 0, 300, 300);
[docController presentOptionsMenuFromRect:rect inView:self.view animated:YES];
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

***由于未捕获的异常'NSGenericException'终止应用程序,原因:' - [UIPopoverController dealloc]到达,而popover仍然可见.

我现在应该怎么做 ?

Chr*_*ies 23

要通过"一次性"UIDocumentInteractionController预览文档,您应该在interactionControllerWithURL之后保留它,并在UIDocumentInteractionControllerDelegate方法documentInteractionControllerDidDismissOptionsMenu中自动释放它.正如David Liu所说,释放它会崩溃.但是自动释放工作.我已经检查过dealloc实际上被调用了.

以下代码应该有效:

__PRE__
- (void)previewDocumentWithURL:(NSURL*)url { UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url]; preview.delegate = self; [preview presentPreviewAnimated:YES]; [preview retain]; } - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller { [controller autorelease]; }

  • 对于使用Open In ...对话的所有人,请使用委托方法documentInteractionControllerDidDismissOpenInMenu: (2认同)

Dav*_*Liu 8

这基本上是旧的内存管理问题.

[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]]返回一个自动释放的对象,因此它会在代码块完成后立即自动释放.我猜这presentModalViewController不会为你保留副本,但这是一个侧面点.

基本上,您需要在代码块结束之前保留它.更烦人的部分是跟踪docController正在做什么,所以你不会泄漏内存.你必须检查结果
[docController presentOptionsMenuFromRect:rect inView:self.view animated:YES];

如果它返回NO,则表示菜单从未显示过,因此您应立即对其进行释放(如果您已经执行了保留).

但是,如果它返回YES,那么你需要为docController实现委托方法,并在菜单被解除时释放它(在这种情况下,它将在:
- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
被调用时).

编辑: 我想在这里进行修正:

如果弹出菜单被取消,上一个答案将崩溃.基本上没有任何好方法来创建一次性DocController.相反,我认为最好只为viewcontroller中的每个文件创建一个,并在完成后解除分配.否则,您将遇到无数可能的情况,DocController将过早发布并崩溃.