2011-05-27 120 views
6

我的主菜单上有一个普通的UIButton,它当前启动了一个UIViewController;相应.m文件的内容如下:UIDocumentInteractionController在退出时崩溃

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    documentPath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"pdf"]; 
    NSURL *targetURL = [NSURL fileURLWithPath:documentPath]; 

    document = [UIDocumentInteractionController interactionControllerWithURL: targetURL]; 
    document.delegate = self; 
    [document retain]; 

    return self; 
} 

-(UIViewController *)documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller 
{ 
    return self; 
} 

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller 
{ 
    [document autorelease]; 
} 

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [document presentPreviewAnimated: YES]; // ** CRASH ** 
} 

-(void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

-(void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

-(void)dealloc 
{ 
    [super dealloc]; 
} 

我的PDF文件加载不如预期,但是当我点击“完成”按钮,文档关闭,我在我的空白留下的UIViewController盯着 - 可以说是为预期。但是,如果我点击导航“返回”按钮,那么应用程序会崩溃,并在我的viewDidLoad方法中发现访问错误,在该方法中找到对presentPreviewAnimated的调用。

如果有人可以请看看,我将非常感激。

(顺便说一句,没有NIB文件被创建这个视图控制器时,是的,这本身就是错误的),我想知道

+0

我认为你的问题可能与你在委托中做的autorelease(和initWithNib方法中的保留一起)。 – onnoweb 2011-05-27 14:32:04

+0

@onnoweb如果没有保留,它会崩溃,如下所示:第1行: - [__ NSCFType presentPreviewAnimated:]:无法识别的选择器发送到实例0x1a32e0 第2行:***由于未捕获异常'NSInvalidArgumentException',原因:' - [__NSCFType presentPreviewAnimated:]:无法识别的选择器发送到实例0x1a32e0'我加入保留的原因是因为Apple告诉我们在UIDocumentInteractionControllerDelegate协议中引用。 – Luke 2011-05-27 14:37:57

+0

嗯......我在其中一个应用程序中使用了UIDocument,不做保留,没有任何问题。如果将UIDocument代码从initWithNib移动到viewDidLoad会怎样? – onnoweb 2011-05-27 14:56:12

回答

1

问题是否是你视图创建过程中做到这一点。所以当用户关闭文档预览时,它会返回到一个不完整的UIView。因此,也许首先构建并加载视图,然后从viewDidAppear中执行UIDocument?