views:

451

answers:

3

Hello,

how do I use multiple UITabBar tabs for controlling a single UIWebView?

An Example:

Pressing tab1: UIWebView loads index.html
Pressing tab2: the same UIWebView loads customers.html
Pressing tab3: the same UIWebView loads tos.html

My approach was to put the UIWebView in the AppDelegate and set self.webview of every ViewController to that single UIWebView. However, the WebView gets displayed, but is white and doesn't load anything.

Any hints?

+1  A: 

WebViews are expensive so your approach is probably right, but you may want to start out with creating a separate webview in each tab view controller and see if things are working as you expect there first. The overhead may start causing your app to start receiving memory warnings, but this would at least allow you to see if your pages will at least load correctly in that context.

Also, can you post some code that shows how you're adding your webview to each view controller? If you are not setting the frame of your webview using -setFrame, once you've added the webview as a subview of your view controller view, it won't show up correctly.

Matt Long
+1  A: 

You need to -removeFromSuperview and then -addSubview to move the webview around; just re-assigning to an instance variable won't do the trick.

Ben Gottlieb
+1  A: 

A tab bar is really the wrong model for what you are trying to do. If you want to switch content what you really want is a ToolBar, because that is meant to alter an-place view in various ways - TabBar is meant for switching between multiple unrelated views, and as such you are fighting against it when you try to use the same view across multiple tabs.

Now if you really have totally unrelated content, why not consider just using different UIWebViews, one per tab. They are not so heavy that loading having multiple instances really hurts anything, and tabs do not load content until pressed so they will not all load at once. To conserve memory you could even toss away view controllers when tabs are switched. But having each view manage its own web view makes more sense and means you can keep the content cached much easier.

Kendall Helmstetter Gelner