1

I have the following piece of code to add a QLPreviewController subview

{
    QLPreviewController *preview = [[QLPreviewController alloc] init];
    preview.delegate = self;
    preview.dataSource = self;
    [self addChildViewController:preview];
    [self.view addSubview:preview.view];
    [preview didMoveToParentViewController:self];
    self.previewController = preview;
}

-(NSInteger) numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return 1;
}

-(id) previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index
{
    return self.url;
}

self.url is an NSURL that is located in NSTemporaryDirectory - file://localhost//.../blah.pdf

My issue is that when my laptop is connected to the internet, the document shows up as a subview, but when my laptop is not connected, numberOfPreviewItemsInPreviewController & previewItemAtIndex do not get called.

  • I've tried a vanilla program with a simple view controller, and it seemed to work fine. (My app is more complex than that).
  • When I try presenting the document as a modal view, it seems to work irrespective of whether or not the simulator is connected to the internet. [self presentViewController:preview animated:NO completion:nil]; --> works consistently.

I need to get the subview working for online & offline modes, it would be great if someone could help!

tc.
  • 33,468
  • 5
  • 78
  • 96
n915
  • 81
  • 1
  • 1
  • 5
  • Try using this link worked for me http://stackoverflow.com/questions/8493419/how-to-add-qlpreviewcontroller-as-subview-in-objective-c – kolaveri May 17 '13 at 07:08

1 Answers1

2

You might be encountering strange behaviour because the QLPreviewController's view is not designed to be embedded in another view. From the QLPreviewController class reference overview:

To display a Quick Look preview controller you have two options: You can push it into view using a UINavigationController object, or can present it modally, full screen, using the presentModalViewController:animated: method of its parent class, UIViewController.

Having said that, you could try:

  1. Forcing the QLPreviewController to (re)display its contents. Try adding [self.previewController reloadData]; to the end of your first method. This should force the data source method(s) to fire.

  2. Forcing the view to "refresh" it's subviews: [self.view setNeedsLayout] (which may in fact force a reloadData like the first option).

Good luck!

gavdotnet
  • 2,214
  • 1
  • 20
  • 30
  • Thanks! I had to force viewDidAppear to make it work, and that seemed to work fine for me. – n915 Mar 28 '13 at 18:23
  • Thanks a ton. Was stuck in this problem for long, [self.previewController reloadData] worked for me perfectly. – nitz19arg Dec 27 '13 at 07:05