English 中文(简体)
Clear UIWebView content upon dismissal of modal view (iPhone OS 3.0)
原标题:

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?

最佳回答

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];
问题回答

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...

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"]]];

evaluating javascript is usually quicker than loading a new request.

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

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.

i prefer to the following:

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




相关问题
How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

What should I load into memory when my app loads?

I have objects I am saving to the file system using serialization. When I load the app, should I load all the objects into memory or just stubs (for searching capabilities)? If I load just stubs, ...

Data Destruction In C++

So, for class I m (constantly re-inventing the wheel) writing a bunch of standard data structures, like Linked Lists and Maps. I ve got everything working fine, sort of. Insertion and removal of ...

Java: Finding out what is using all the memory

I have a java application that runs out of memory, but I have no idea which code is allocating the memory. Is there an application with which I can check this? I use Eclipse.

View ram in DOS

Is there a way in dos (im using a dos boot disk on a linux machine) to view portions of ram? ie. some form of command to read the binary at a given address? edit: my bootable floppy doesnt have ...

does argument in printf get located in memory?

in c, when I write: printf("result %d ",72 & 184); Does "72 & 184" get a a block in memory (for example 72 takes 4 bytes, 184 takes 4 bytes?...)

热门标签