Eugene Gordin
Eugene Gordin

Reputation: 4107

How to swap two UIWebViews in iOS

In my app I have a sliding view that holds UIWebView, that I want to substitute with another UIWebView that has already loaded content on user tap instead of loading the current view.

I tried just simply assign one view to another:

UIWebView* webView1 = [[UIWebView alloc] initWithFrame:frame];
UIWebView* webView2 = [[UIWebView alloc] initWithFrame:frame];

webView1 = webView2; // didn't work

Any help is highly appreciated!

Upvotes: 0

Views: 93

Answers (3)

Thanh-Nhon Nguyen
Thanh-Nhon Nguyen

Reputation: 3428

My solution:

  1. Get the whole HTML string of the webView2:

    NSString *htmlString = [webView2 stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];

  2. Load this HTML string to webView1.

Upvotes: 0

Jason Barker
Jason Barker

Reputation: 3020

If you need to keep them both around, you could just hide the one that should go away:

[webView1.superview addSubview: webView2];
[webView1 setHidden: YES];

Upvotes: 2

Jason Barker
Jason Barker

Reputation: 3020

Try this:

[webView1.superview addSubview: webView2];
[webView1 removeFromSuperview];

Upvotes: 0

Related Questions