Bharathi
Bharathi

Reputation: 485

Use same UIWebview in 2 viewcontrollers as a subview

I need to use same UIWebview instance in the 2 viewcontrollers as a subview. But when i add the webview to the second view controller, its removing from the first view controller. Both the view controllers will be visible at the same time. And I need to retain the same webview in the 2 view controllers.

I have tried of Archiving and unarchiving option. but the loaded details in the webview was not displayed when doing in this way. Please suggest for any options.

Upvotes: 1

Views: 344

Answers (1)

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

You are just creating a new pointer to the same UIView object. Since UIViews do not respond to copy, you will need to actually allocate and init a new UIView and customize it to match the first one

A view can only be contained in a single parent view's hierarchy. When you add it to a new one, it is removed from the previous one.

So, You can't use same Web view in both View Controllers. You have to create another one. You can't add same instance multiple time.

You can achieve this by following pattern:

Create a common class where you can create UIWebview and load your URL and then add it to your controller. So you don't need to write url for every UIWebView.

Upvotes: 1

Related Questions