【发布时间】:2012-10-25 07:15:42
【问题描述】:
我遇到了许多从本地 html 文件加载和重新加载 UIWebView 内容的方法,但似乎我一直遇到的问题。
在我的项目中,我有一个 webView,它可以动态加载从我的 XCode 项目中的 50 个不同 HTML 文件之一中提取的 HTML 内容。 根据用户之前在 viewController 上按下的按钮,使用不同的 HTML 文件重新加载我的 webView 的内容。大部分时间一切正常,但在调用 webViewDidFinishLoad 委托方法后,webView 会不时显示“(null)”。
我不明白发生了什么,当我在这个 viewController 上来回切换并重新加载 webView 时,HTML 内容最终会正确显示同一个 HTML 文件。
这是在 webView 中加载 HTML 的代码(从 viewWillAppear 方法调用):
- (void) reloadWebViewFromLocalFile{
// Reset the UIWebView
[self.contentWebView removeFromSuperview];
self.contentWebView = nil;
self.contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
[self.contentWebView setContentMode:UIViewContentModeScaleAspectFit];
[self.contentWebView setDelegate:self];
[self.contentScrollView addSubview:self.contentWebView];
[self.contentWebView release];
NSString *fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d.%d",self.currentIndexPath.section+1,self.currentIndexPath.row+1] ofType:@"html"];
NSString *CSSContent = [NSString stringWithFormat:@"<style type=\"text/css\">body{padding-left:8px;padding-right:8px;font-family:\"%@\";}p{text-align:justify;}img{max-width:300px;display:block; margin:auto;}strong{font-family:\"%@\";}h1{font-size:20;font-family:\"%@\"}h2{font-size:18;font-family:\"%@\"}h3{font-size:17;font-family:\"%@\"}</style><script type=\"text/javascript\">touchMove = function(event) {event.preventDefault();}</script>", FONT_STAG_BOOK, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD];
self.HTMLString = [NSString stringWithFormat:@"<html><head>%@</head>%@</html>",CSSContent,[NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:fileName] bytes]]];
[self.contentWebView loadHTMLString:self.HTMLString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}
这是webViewDidFinishLoad委托方法:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *stringContent = [self.contentWebView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
float height = [[self.contentWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue] + 20;
[self.contentWebView setFrame:CGRectMake(0, 0, 320, height)];
[self.contentScrollView setContentSize:CGSizeMake(0, height+self.nextButton.frame.size.height)];
[self.nextButton setFrame:CGRectMake(0, height, 320, self.nextButton.frame.size.height)];
if ([self.contentWebView respondsToSelector:@selector(scrollView)]) {
self.contentWebView.scrollView.scrollEnabled = NO; // available starting in iOS 5
} else {
for (id subview in self.contentWebView .subviews)
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).scrollEnabled = NO;
}
}
编辑:忘记复制粘贴在 webView 中实际加载 HTML 字符串的行
【问题讨论】:
-
实际加载网页视图的代码在哪里?另外,我认为你不应该发布它,因为你将它分配给一个为你做所有事情的属性,即使没有 ARC...
-
刚刚编辑了帖子以添加实际加载 webView 的缺失行。我去掉了release语句,在webView的分配后添加了一个autorelease
-
在实际加载 HTML 之前执行
NSLog(@"%@", [NSData dataWithContentsOfFile:fileName]);会输出什么?当它说 (null) 我的意思是...... -
我使用以下方法打印 HTML 字符串: NSLog(@"%@",[NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:fileName] bytes]]); HTML 内容在这里,我有“ bla bla bla ”,但在结束
标签: objective-c ios uiwebview uiwebviewdelegate