关闭模态视图时清除 UIWebView 内容 (iPhone OS 3.0)

发布于 2024-09-03 13:06:17 字数 726 浏览 13 评论 0原文

我目前有一个显示在模式视图中的 UIWebView。它基本上是一个详细视图,当用户单击链接时提供页面视图。当视图被关闭然后再次打开时(当用户单击另一个链接时),先前加载的内容仍然可见,并且新内容加载在最后内容的“顶部”。这是有道理的,因为 UIWebView 的实例在会话之间持续存在,并且仅在需要内存时才被释放。

但是,我想在模式视图关闭时完全清除 UIWebView,以便 1)清除内容并 2)释放内存。到目前为止我的研究和尝试还没有找到答案。这些链接对我不起作用:

  1. 是否可以释放内存UIWebView?
  2. 重用 UIWebView在 iPhone 上短暂显示之前加载的内容

我尝试过 [[NSURLCache sharedURLCache] removeAllCachedResponses]; 并将 webView 设置为 nil 并在 modal-view-dismiss 时手动释放 webView无济于事。干瘪的群众有什么想法吗?

I currently have a UIWebView that is displayed within a modal view. It is basically a detail view that provides a view of a page when the user clicks a link. When the view is dismissed and then brought up again (when the user clicks another link), the previously-loaded content is still visible and the new content loads "on top" of the last content. This makes sense because the instance of the UIWebView persists between sessions and is only released when the memory is needed.

However, I would like to completely clear the UIWebView when the modal view is dismissed so that 1) content is cleared and 2) memory is freed. Thus far my research and attempts have not found an answer. These links haven't worked for me:

  1. is it possible to free memory of UIWebView?
  2. Reused UIWebView showing previous loaded content for a brief second on iPhone

I've tried [[NSURLCache sharedURLCache] removeAllCachedResponses]; and setting the webView to nil and manually releasing the webView upon modal-view-dismiss to no avail. Any thoughts from the wizened masses?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清风疏影 2024-09-10 13:06:18

评估 javascript 通常比加载新请求更快。

[webview stringByEvaluatingJavaScriptFromString:@"document.open();document.close();"];

evaluating javascript is usually quicker than loading a new request.

[webview stringByEvaluatingJavaScriptFromString:@"document.open();document.close();"];
浪漫人生路 2024-09-10 13:06:18

就我而言,我需要立即清除网络视图的内容,并能够立即开始加载其他内容。加载空文档 HTML 字符串或使用 JavaScript 清除页面都没有达到预期的效果。相反,我使用:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://stackoverflow.com"]]];

这将视图清除为默认背景并按预期启动下一页的加载。

我希望这也对其他人有帮助。

In my case I needed to clear the web view from its contents instantaneously and be able to start loading something else right away. Neither loading an empty document HTML string nor clearing the page with JavaScript resulted in desired effect. Instead I used:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://stackoverflow.com"]]];

And this cleared the view to the default background and initiated next page's loading as expected.

I hope this helps someone else as well.

老街孤人 2024-09-10 13:06:18

我更喜欢以下内容:

[m_webView stringByEvaluatingJavaScriptFromString:@"location.replace('about:blank')"]

i prefer to the following:

[m_webView stringByEvaluatingJavaScriptFromString:@"location.replace('about:blank')"]
身边 2024-09-10 13:06:17

有时 loadHTMLString 太慢。您还可以使用:

[webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML = \"\";"];

我不确定这是否会释放大量内存......

Sometimes loadHTMLString is too slow. You also can use:

[webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML = \"\";"];

I'm not sure if that will free up much memory though...

碍人泪离人颜 2024-09-10 13:06:17

发布网络视图可能是最好的方法。如果视图位于视图层次结构中(具有超级视图),则必须调用removeFromSuperview 来让超级视图释放该视图。

您还可以为空文档加载 html 字符串:

[webView loadHTMLString:@"<html><head></head><body></body></html>" baseURL:nil];

Releasing the web view is probably the best approach. If a view is in a view hierarchy (has a superview) you must call removeFromSuperview to get the superview to release the view.

You could also load an html string for an empty document:

[webView loadHTMLString:@"<html><head></head><body></body></html>" baseURL:nil];
ま昔日黯然 2024-09-10 13:06:17

在某些情况下,drawonward 和 rob 建议的方法可能不起作用(可能与正在清除的页面上的 CSS 有关),但是这确实有效:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];

It seems under some circumstances the methods suggested by drawnonward and rob may not work (possibly something to do with CSS on the page that's being cleared), however this did work :

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文