QLPreviewController and NSFetchedResultsController objectAtIndexPath returning nil
-
16-03-2021 - |
Question
This code works at first touch. Preview starts to display (Title, Done, Actions, and blank page) and then previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index
is called again. This time [self.resultsController objectAtIndexPath:selectedIndexPath];
returns nil - My guess is the entry is no longer selected. index
is correct at 1.
The procedure:
- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index
{
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
Manuals *manuals = [self.resultsController objectAtIndexPath:selectedIndexPath];
NSURL *vUrl = [self locateUrl:[NSURL URLWithString:manuals.Url] ofType:[manuals.Url pathExtension]];
return vUrl;
}
If I ignore the exception - the preview continues to display the the document.
I did try to keep the last good returned selectedIndexPath, but then received the error: -[__NSArrayM indexAtPosition:]: unrecognized selector sent to instance 0x1cd52800
The question:
How can I get my resultsController object using only the passed in index?
Solution 2
The problem was in numberOfPreviewItemsInPreviewController:previewController
. I was returning the [resultsController count]
, not the number of documents I wanted to be previewed. When the previewController tried to access the next few objects, it was returning nil.
// Returns the number of items that the preview controller should preview
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
{
return 1;
}
OTHER TIPS
I'm not sure if this answers your question but you can use:
[previewController setCurrentPreviewItemIndex:0];
to set the current item that you want to be previewed.