iOS - Opening a PDF via Quicklook without using UIScrollView

我是研究僧i 提交于 2019-12-10 11:48:05

问题


I am trying to open a PDF via the QuickLook framework without using UIScrollView...

I believe I'm missing something...

Where I believe I'm going wrong is that I need to use a QLPreviewController and on the QLPreviewController is a dataSource that has to conform to QLPreviewItem. The documentation states that NSURL does conform to QLPriewItem so I'm setting the preview.dataSource to an NSURL which is throwing an error:

[NSURL numberOfPreviewItemsInPreviewController:]: unrecognized selector sent to instance

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL numberOfPreviewItemsInPreviewController:]: unrecognized selector sent to instance 0x5b5f200'

Which makes me think that NSURL does not conform.

all the code I think is necessary...

- (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item {

    return YES;
}

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {

    return [documents count];
}

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {

    return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
}

- (void)pushPDF {

    QLPreviewController *preview = [[QLPreviewController alloc] init];
    preview.dataSource = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MCIT_Quiz" ofType:@"pdf"]];
    //preview.currentPreviewItemIndex = 0;
    [self presentModalViewController:preview animated:YES];
    [preview release];
}

回答1:


I ended up just creating another class to hold my values and use as a datasource, a bit quick and dirty but it works.

//
//  documentList.h
//

#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>


@interface DocumentList : NSObject <QLPreviewControllerDataSource, QLPreviewControllerDelegate> {
    NSArray *documents;
}

@property (nonatomic, retain) NSArray *documents;

-(void)createList;
-(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller;
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;

@end

inserting text to break up the files

//
//  documentList.m
//

#import "DocumentList.h"

@implementation DocumentList

@synthesize documents;

-(void) createList {

    documents = [[NSArray arrayWithObjects:@"Quiz.pdf", nil] retain];
}

-(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {

    return [documents count];
}

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {

return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
}

@end



回答2:


Well, I don't see where an NSURL conforms to QLPreviewControllerDataSource. I think you want

 preview.dataSource = self;

And then your already written routines (numberOfPreviewItemsInPreviewController and previewController) would return the appropriate NSURL (although it's not clear how "documents" gets filled.).



来源:https://stackoverflow.com/questions/5819232/ios-opening-a-pdf-via-quicklook-without-using-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!