UIDocumentInteractionController的奇怪问题 [英] Strange issue with UIDocumentInteractionController
问题描述
我不知道这段代码有什么问题,但每当我运行应用程序时,在显示菜单后,应用程序崩溃。
NSString * path = [[NSBundle mainBundle] pathForResource:@tungofType:@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];
我收到错误:
***由于未捕获的异常'NSGenericException'终止应用程序,
原因:' - [UIPopoverController
dealloc]达到,而popover仍然可以看到
。'
我现在该怎么办?
要通过一次性UIDocumentInteractionController预览文档,您应该在interactionControllerWithURL之后保留它,并在UIDocumentInteractionControllerDelegate方法documentInteractionControllerDidDismissOptionsMenu中自动释放它。正如David Liu所说,释放它会崩溃。但是自动释放工作。我已经检查过dealloc实际上是被调用的。
以下代码应该可以工作:
- (void)previewDocumentWithURL:(NSURL *)url
{
UIDocumentInteractionController * preview = [UIDocumentInteractionController interactionControllerWithURL:url];
preview.delegate = self;
[预览presentPreviewAnimated:YES];
[预览保留];
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
[controller autorelease];
}
I don't know what wrong with this code but everytime when I run the app, after the Menu is shown, the app crash.
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];
Error I got:
*** Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.'
What should I do now ?
To preview a document via a "throwaway" UIDocumentInteractionController you should retain it after interactionControllerWithURL and autorelease it in the UIDocumentInteractionControllerDelegate method documentInteractionControllerDidDismissOptionsMenu. As remarked by David Liu, releasing it will crash. But autoreleasing works. I have checked that dealloc is actually called.
The following code should work:
- (void)previewDocumentWithURL:(NSURL*)url
{
UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
preview.delegate = self;
[preview presentPreviewAnimated:YES];
[preview retain];
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
[controller autorelease];
}
这篇关于UIDocumentInteractionController的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!