Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIDocumentInteractionController在退出时崩溃_Iphone_Ios_Uiviewcontroller_Uidocumentinteraction - Fatal编程技术网

Iphone UIDocumentInteractionController在退出时崩溃

Iphone UIDocumentInteractionController在退出时崩溃,iphone,ios,uiviewcontroller,uidocumentinteraction,Iphone,Ios,Uiviewcontroller,Uidocumentinteraction,我的主菜单上有一个常规的UIView按钮,当前启动UIViewController;相应的.m文件的内容如下: -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; documentPath = [[NSBundle mainBundle

我的主菜单上有一个常规的UIView按钮,当前启动UIViewController;相应的.m文件的内容如下:

-(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方法中出现了一个错误的访问错误,在这里可以找到对PresentPreviewInMatd的调用

如果有人能看一下,我将不胜感激


(顺便说一句,创建此视图控制器时没有NIB文件。是的,这本身就是错误的)

我想知道问题是否在于您在创建视图时这样做。因此,当用户关闭文档预览时,它将返回到未完全形成的UIView。因此,也许首先构建并加载视图,然后才显示UIDocumentfromViewDid

我认为您的问题可能在于您在委托中执行的自动释放(以及initWithNib方法中的retain)。@onnoweb如果没有retain,它将与以下内容一起崩溃:第1行:-[\u NSCFType presentPreviewAnimated:]:无法识别的选择器已发送到实例0x1a32e0第2行:**由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\u NSCFType presentPreviewAnimated:]:无法识别的选择器发送到实例0x1a32e0'我添加保留的原因是因为Apple在UIDocumentInteractionControllerDelegate协议引用中告诉我们要保留。嗯。。。我在我的一个应用程序中经常使用UIDocument,不做任何保留,也没有任何问题。如果您将UIDocument代码从initWithNib移动到viewDidLoad,该怎么办?我想知道问题是否在于您在创建视图时这样做。因此,当用户关闭文档预览时,它将返回到未完全形成的UIView。所以,也许首先构建并加载视图,然后从视图中创建UIDocument?完美的朋友!将UIDocumentInteractionController的初始化代码放在ViewDidDisplay中:与跟踪它是否是第一次查看的BOOL一起使用,效果非常好。如果是第一个视图,则创建并推送文档。当我点击“完成”时,ViewDid再次出现,只是现在我的BOOL显示为NO(不是第一个视图),所以我称之为popToRootViewControllerAnimated:这很有效。感谢您对本查询的支持。