将viewWillAppear推迟到webViewDidFinishLoad [英] Deferring viewWillAppear until webViewDidFinishLoad

查看:118
本文介绍了将viewWillAppear推迟到webViewDidFinishLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在多个视图控制器中使用 UIWebViews 的应用程序。 UIWebViews 用于呈现本地生成的html,不需要慢速网络访问。

I have an application that uses UIWebViews in several view controllers. The UIWebViews are used to render locally generated html, no slow network access required.

为了节省内存我只加载这些按需按照viewcontroller viewWillAppear 回调的提示。 (并卸载屏幕外实例以响应 didReceiveMemoryWarning 消息。)

To save memory I only load these on demand as prompted by the viewcontroller viewWillAppear callback. (And unload offscreen instances in response to didReceiveMemoryWarning messages.)

问题是用户可以看到html被渲染,有时伴随着闪光的造型和其他各种令人不快的文物。我更愿意在屏幕外完成渲染,并在准备就绪时显示完全渲染的视图。

The problem is that the user gets to see the html being rendered, sometimes accompanied by flashes of styling and other assorted unpleasant artifacts. I would much rather the rendering be done offscreen, and reveal the fully rendered view when its ready.

能够拥有<$ c $非常整洁c> viewWillAppear 在UIWebView完全呈现之前不会返回。但是如何?

It would be very tidy to be able to have the viewWillAppear not return until the UIWebView is fully rendered. But how?

我告诉 UIWebView 通过发送 loadHTMLString来呈现的内容: baseURL:消息。这是异步的,有一段时间(很快)后来webview的委托被回调 webViewDidFinishLoad

I tell the UIWebView what to render by sending it a loadHTMLString:baseURL: message. This is asynchronous, and some time (soon) later the webview's delegate gets called back webViewDidFinishLoad.

我试验过在 viewWillAppear 中运行runloop,运行 NSDefaultRunLoopMode UITrackingRunLoopMode 。这可以在模拟器中工作(它在日志中投诉

I experimented with running a runloop inside viewWillAppear, running either the NSDefaultRunLoopMode or UITrackingRunLoopMode. This works in the simulator (it complains to the log


在交易中调用的[CATransaction synchronize]

[CATransaction synchronize] called within transaction

但确实有效)但在设备上它会死锁, webViewDidFinishLoad 永远不会被调用。

but does work) but on a device it deadlocks, with webViewDidFinishLoad never being called.

(另外,似乎 UIWebView loading属性不起作用。至少,在我调用 loadHTMLString之后: baseURL:并且在收到回调之前并不是真的。)

(Also, it seems like the UIWebView loading property doesn't work. At least, after I call loadHTMLString:baseURL: and before getting the callback it's not true.)

推荐答案

我认为这里有很多解决方案。一个快速的方法是加载UIWebView,将其隐藏属性设置为YES。然后将UIViewController设置为UIWebViews委托并实现:

Lots of solutions here I think. A quick one is to load your UIWebView with it's hidden property set to YES. Then set your UIViewController as the UIWebViews delegate and implement:

- (void)webViewDidFinishLoad:(UIWebView *)webView

将属性设置为NO。

需要注意的是webViewDidFinishLoad将触发超过一旦你有框架/嵌入内容。所以你必须跟踪这个。如果您要加载本地内容,那不应该是一个问题。

A thing to note is that webViewDidFinishLoad will fire more than once if you have framed/embedded content. So you have to keep track of this. Shouldn't really be a problem if you are loading local content.

这篇关于将viewWillAppear推迟到webViewDidFinishLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆