0
votes

I've embedded a QLPreviewController as a sub view controller in a view controller of mine and have setup the delegate and data source for it:

qlPreviewController.delegate = self
qlPreviewController.dataSource = self

In my viewDidLoad, I'm downloading a remote document. Once it's finished downloading, I reload the QLPreviewController to display that document. This works fine.
Now, I want to show multiple documents at the same time. Therefor, I download multiple documents, setup an array of those and then reload the QLPreviewController.

Here's the implemented data source methods:

func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
    print("count:", downloadedDocuments.count)
    return downloadedDocuments.count
}

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    print("\(index+1)/\(downloadedDocuments.count), id:", downloadedDocuments[index].id)
    return downloadedDocuments[index].localURL as QLPreviewItem
}

What ends up happening is that only one document is being display, which is the first in the array. I added the print statement above, to verify that there are multiple documents in the downloadedDocuments array. It prints:

count: 5
1/5, id: 251

As you can see, it's calling previewItemAtIndex only once, and the index remains 0.
Why is that? Do I need to setup anything else?

2

2 Answers

1
votes

Your whole title, about “loading multiple files”, suggests a misconception. I think you’re assuming that a QLPreviewController is a kind of collection view controller that displays multiple objects simultaneously. It isn’t. It previews one document. If it has multiple preview items, the user can page sideways to another document, or choose from the TOC menu, but it’s still showing just one document at a time.

Also, you seem to be trying to use the preview controller as an embedded. child view controller. You can’t. It can be presented, or pushed onto a navigation controller; that’s all. Don’t try to retain or reuse it. Don’t use its view as a subview. When it’s time to preview, create the controller and present or push it. When the controller is dismissed or popped, let it die.

0
votes

Turns out, I misunderstood how multiple items are loaded in a QLPreviewController.
I thought, the user had to scroll vertically and see the side bar with thumbnails. This is only for pages within a single document, however.

I had to scroll horizontally.
Thanks to @matt, who cleared this up for me.