Search code examples
iosquicklookqlpreviewcontroller

QLPreviewController sometimes does not show the document


I have a master-detail app where the master shows a list of documents, and the detail shows a preview of these documents.

In the storyboard I have draw a UIView called vistaPreview. I have declared it in .h:

@property (weak, nonatomic) IBOutlet UIView *vistaPreview;

I´ve also declared the variable QLPreviewController* previewVC; to be used in .m

In .m, I have a method that is called when the user touch one document in the list. The method add a QLPreviewController in the view vistaPreview:

    previewVC = [[QLPreviewController alloc] init];
    previewVC.dataSource = self;
    previewVC.delegate = self;
    [self addChildViewController:previewVC];
    CGFloat w= self.vistaPreview.frame.size.width;
    CGFloat h= self.vistaPreview.frame.size.height;
    previewVC.view.frame = CGRectMake(0, 0,w, h);
    [self.vistaPreview addSubview:previewVC.view];
    [previewVC didMoveToParentViewController:self];

The thing is, after several times clicking some documents in the list, I get the next error:

Domain=XPCObjectsErrorDomain Code=2 "The operation couldn’t be completed.

The app doesn't crash, I only get a message in the QLPreviewController view with the name of the document (and not the content). When I click over another document in the list, the app returns to work fine showing the documents content.

Any idea of how to fix it?

Thanks in advance!


Solution

  • The problem was the amount of QLPreviewController children. So then, I look for QLPreviewController children, and remove them from the parentViewController.

    NSArray *children = [self childViewControllers];
    for (int i = 0; i<[children count]; i++) {
        NSString *classString = [NSString stringWithFormat:@"%@", [[children objectAtIndex:i] class]];
        if ([classString isEqualToString:@"QLPreviewController"]) {
            [[children objectAtIndex:i] removeFromParentViewController];
        }
    }