2011-04-28 58 views
0

我試圖打開通過QuickLook的框架PDF不使用的UIScrollView ...iOS版 - 通過開一個的Quicklook PDF不使用的UIScrollView

我相信我失去了一些東西......

我在哪裏相信我會錯誤的是,我需要使用QLPreviewController,並且在QLPreviewController上是一個必須符合QLPreviewItem的數據源。該文件指出,NSURL不符合QLPriewItem所以我設置preview.dataSource到NSURL它拋出一個錯誤:

[NSURL numberOfPreviewItemsInPreviewController:]:無法識別的選擇發送到實例

終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [NSURL numberOfPreviewItemsInPreviewController:]:無法識別的選擇器發送到實例0x5b5f200'

這使我認爲NSURL不符合。

所有我認爲是必要的代碼...

- (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

我最終創建了另一個類來保存我的值並將其用作數據源,雖然有點快捷但很髒,但它起作用。

// 
// 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 

插入文本,打破了文件

// 
// 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 
+0

如果你真的只會有一個,那麼爲什麼不擺脫文件,只是返回1並將quiz.pdf放入previewItemAtIndex – mackworth 2011-04-29 00:15:20

+0

這是否工作?該文檔指出它需要一個int而不是一個文件名。 – Mytheral 2011-04-29 11:49:59

+0

啊,我明白你的意思了。 Quiz.pdf的意思是動態的,上面只是一個簡單的例子。 – Mytheral 2011-04-29 11:59:08

1

嘛,我看不出一個NSURL符合QLPreviewControllerDataSource。我想你想

preview.dataSource = self; 

然後你已經寫程序(numberOfPreviewItemsInPreviewController和previewController)將返回相應的NSURL(雖然現在還不清楚,「文件」是如何得到填補。)。

+0

,當類本身符合工作......但是這就是我想要不要做。 – Mytheral 2011-04-28 19:04:42

+0

嗯,我認爲我的觀點仍然是正確的,即你的代碼必須是提供預覽項目的數據源,不管你是否選擇將它分解到另一個類中。 – mackworth 2011-04-29 00:13:28

+0

任何可以告訴我如何定製這(QLPreviewController)? 我想在這裏爲用戶提供一些關於文本搜索,打印和下一個和上一個按鈕的選項。 – sandy 2011-06-24 10:15:09