开发者

How can I prevent delayed display of my UIWebView

I must include some 'rich text' instructions* preceding a form; and thought adding a UIWebView to the header of the tableview would work. The HTML is a small file in my resources folder, the CSS is in a style tag in the file; so it's all nice and self contained.

The problem is, the view transitions in; then after a small delay the contents of the webview appear. The effect is jarring, and I don't think hiding the view and fading it in when it's ready would be any more desirable.

I'm creating the view with the code below, in my viewDidLoad method.

UIWebView * wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, HEADER_HEIGHT)];
[wv setBackgroundColor:[UIColor clearColor]];
[wv setOpaque:NO];
NSString *path = [[NSBundle mainBundle] pathForResource:@"HeaderMsg" ofType:@"html"];
assert(path != nil);
NSData *htmlData = [NSData dataWithContentsOfFile:path];
[wv loadData:htmlData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil];
[self.tableView setTableHeaderView:wv];
[wv release];

I found 2 ugly workarounds, but I'm hoping someone has a better solution; since my workarounds make a real mess out of the code:

  1. On previous screen when you make the UIMYSCREENViewController, call a [vc preloadWebViewWithDelegate:self]; method which will make the webview using the caller as the delegate. The caller then retains the vc and waits for the webview to sent it a webviewDidFinishLoad method, at which time it can present the view and release the vc.

  2. The calling view can make the webview, wait for it to finish, then create the new view and pass the webview into it.

At any rate, both of those "solutions" make me gag a little, so I'm hoping others have found a better way.

(*T开发者_如何转开发he instructions are mostly simple, styled text with some bullet points (no images or overly aggressive styling); but it takes about 14 carefully aligned UILabel views to simulate this without a webview - and is subject to the whims of the customer wanting to change the message.)


I would create another independent model or controller object to create and retain the webview, hopefully at a higher level and before (maybe during app init) displaying the view with the UI that could bring up the webview.

Consider this the same as pre-staging resources for an action game so that they don't have to be loaded during the game loop, which is a common design pattern.


Consider using this https://github.com/Cocoanetics/DTCoreText/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜