本文介绍了UIWebView延迟加载本地内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIWebView中显示了少量的本地内容,大致显示了3段文字和一张100x100px的图像.将UIWebView推送到导航控制器时,大约需要半秒钟才能显示内容.这发生在设备上,而不是在模拟器中.显然,在显示内容之前,UIWebView需要做一些工作,但是在推送视图出现之前,我是否可以通过某些方法来做到这一点?我自己做这件事没有任何运气.

I'm displayed a small amount of local content in a UIWebView, roughly 3 paragraphs of text and one 100x100px image. When the UIWebView is pushed onto to the nav controller, it takes about half a second before the content is displayed. This occurs on the device, not in the simulator. Obviously the UIWebView needs to do some work before displaying the content, but is there some way I can do this before the pushed view appears? I have not had any luck doing this myself.

在这里,我将创建并推送包含UIWebView的视图控制器:

Here is where I'm creating and pushing the view controller which contains the UIWebView:

ArticleViewController* viewController = [[ArticleViewController alloc] init];
viewController.article = article;
[viewController view]; //touch the view so it gets loaded
[viewController loadWebView]; //load the web view content
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];

这是loadWebView方法:

-(void)loadWebView {
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];

    NSString* content = article.content;

    NSString *html = [NSString stringWithFormat:@"\
                      <html><body style='background-color: transparent'><style type=""text/css"">body {font-family: helvetica; color: black; font-size: 10pt; margin: 10px 10px 10px 10px}</style>\
                      %@<div style='text-align:center'><a href='13443574_9709e4cf37.jpg?photographer=test' style='-webkit-tap-highlight-color:rgba(0,0,0,0);'><img src='13443574_9709e4cf37.jpg' height='160' width='230'></a></div></body></html>", content];
    [self.webView loadHTMLString:html baseURL:baseURL];
}

以前我在viewDidLoad方法中有[viewController loadWebView],但是结果似乎是相同的.按下viewController时出现黑屏,然后半秒钟后加载内容.

Previously I had [viewController loadWebView] in the viewDidLoad method, but the result seems to be the same. A blank screen when the viewController is pushed, followed by the content loading half a second later.

有什么想法吗?

推荐答案

您要让用户等待,唯一的问题是:它出现在Webview之前还是之后?如果将其设置为"before",则应该创建控制器,加载Web视图,但是要等到-webViewDidFinishLoad:委托方法触发后再推送它.收到通知后,您就可以推送视图.

You're going to make the user wait, the only question is: is it before or after the webview appears? If you're set on "before", then you should create the controller, load the web view, but wait to push it until the -webViewDidFinishLoad: delegate method fires. Once you receive that, you can push the view.

这篇关于UIWebView延迟加载本地内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:43