2

在此处输入图像描述

我在我的应用程序的 Resourse 文件夹的子文件夹中有一些 PDF 格式文档。我尝试以这种方式阅读这些文档。这是我的代码。

   NSString *path = [[NSBundle mainBundle] pathForResource:chapter ofType:@"pdf"];
   NSURL *targetURL = [NSURL fileURLWithPath:path];
   NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
   [[webView scrollView] setContentOffset:CGPointMake(0,500) animated:YES];
   [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]];
   [webView loadRequest:request];
   [self.view addSubview:webView];
   [webView release];

在此代码中“路径”返回零值,当我签入控制台时。但是当我检查存储我想要的 pdf 文档名称的字符串“章节”时,它总是有一个 pdf 文档名称。当我的程序崩溃时在控制台中它显示了这样的东西。

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[UIWebView scrollView]: unrecognized selector sent to instance 0x6587d20'

任何帮助都会被给予。

4

2 回答 2

3

你在你的项目中检查你的 pdf 文件吗?

在此处输入图像描述

我测试了以下代码。没问题。

请检查您项目中的 chapter.pdf 文件。点是相对路径。

必须区分大小写。“章节”/“章节”不等于字符串。

NSLog(@"path:%@",[[NSBundle mainBundle] pathForResource:@"chapter" ofType:@"pdf"]);


console:
    /var/mobile/Applications/72AFA069-D83E-469A-A687-AEAFDB0B4D97/TableViewTest.app/chapter.pdf

NSLog(@"path:%@",[[NSBundle mainBundle] pathForResource:@"Chapter" ofType:@"pdf"]);


console:
    (null)

self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
self.myWebView.backgroundColor = [UIColor whiteColor];
self.myWebView.scalesPageToFit = YES;
self.myWebView.delegate = self;
[self.view addSubview:self.myWebView];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"chapter" ofType:@"pdf"]]]];

在此处输入图像描述


在您的项目文件夹中,名称将在实际的 iPhone 包中消失。您创建了一个名为 Resource-Common 的文件夹,但最终在 NSBundle 中消失了。

在此处输入图像描述

见下图。两者都是集成的。项目中的文件夹名称只是 xcode 中的可见文件夹。

在此处输入图像描述

如果你想要 NSBundle 中的所有 pdf 文件列表。尝试以下代码。

NSArray *pdfCollection = [[NSBundle mainBundle] pathsForResourcesOfType:@"pdf" inDirectory:nil];

在此处输入图像描述

于 2012-08-04T11:31:58.123 回答
0

根据 Apple 的文档,UIWebView 的 scrollView 是只读属性:与 Web 视图关联的滚动视图。(只读)。但是你把它当作一个 UIScrollView 来对待,但它不是。那是错误信息。

于 2012-08-04T16:19:51.880 回答