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文件被創建這個視圖控制器時,是的,這本身就是錯誤的),我想知道
我認爲你的問題可能與你在委託中做的autorelease(和initWithNib方法中的保留一起)。 – onnoweb 2011-05-27 14:32:04
@onnoweb如果沒有保留,它會崩潰,如下所示:第1行: - [__ NSCFType presentPreviewAnimated:]:無法識別的選擇器發送到實例0x1a32e0 第2行:***由於未捕獲異常'NSInvalidArgumentException',原因:' - [__NSCFType presentPreviewAnimated:]:無法識別的選擇器發送到實例0x1a32e0'我加入保留的原因是因爲Apple告訴我們在UIDocumentInteractionControllerDelegate協議中引用。 – Luke 2011-05-27 14:37:57
嗯......我在其中一個應用程序中使用了UIDocument,不做保留,沒有任何問題。如果將UIDocument代碼從initWithNib移動到viewDidLoad會怎樣? – onnoweb 2011-05-27 14:56:12