Preload views of UITabBarController in iPhone

Question: I have an iPhone xCode 3.1 project that has a bottom menubar with four tabs. The 2nd and 3rd views display UIWebView content from my web site. When connected to EDGE or 3G, these views don’t load fast and the user has to wait several seconds while these pages are loading. Can I preload both of the UIWebViews? Answer: Yes. While the user is watching the first view you can preload the others in the background. The UITabBarController class lets you manipulate the view controllers and their views. In the applicationDidFinishLaunching event of the class that implements the UIApplicationDelegate protocol you can get the view controllers, so the views. You have several options. Here you can directly call your web page loader or use a more sophisticated notification service or write your own delegate (callback):
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    //  ...
    // Delayed preload
    [NSTimer scheduledTimerWithTimeInterval: 2.0f
                                     target: self
                                   selector: @selector(InitViews:)
                                   userInfo: nil
                                    repeats: NO];
}

// Preload tab's remote web pages
- (void) InitViews: (NSTimer *) timer
{
    UITabBarController *myTabBar = self.tabBarController;   // this is the tab bar from the nib file

    NSArray *myViewControllers = myTabBar.viewControllers;
    if( myViewControllers && [myViewControllers count] > 3 ){
        SecondViewController *ctrl2 = (SecondViewController *)[myViewControllers objectAtIndex: 1];
        [ctrl2 LoadMyWebView];   // direct method call
        ThirdViewController *ctrl3 = (ThirdViewController *)[myViewControllers objectAtIndex: 2];
        [ctrl3 LoadMyWebView];   // direct method call
        // UIWebView *view = (UIWebView *)ctrl2.view; // access the view property if you want
    }
}
Do not forget to include the UIWebViewDelegate in your view controller’s header file:
@interface SecondViewController : UIViewController <UIWebViewDelegate> {
In this example the SecondViewController must have or must have access to the UIWebView:
- (void) LoadMyWebView
{
    // Get your UIWebView, for example:
    UIWebView *myView = (UIWebView *)self.view;
    myView.delegate = self;   // To be sure...
   
    [myView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.weston-fl.com/"]];
}

// If you need to do something while loading these pages...
#pragma mark UIWebView delegate methods

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    // starting the load, show the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // finished loading, hide the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    // load error, hide the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
   
    // report the error inside the webview
    NSString* errorString = [NSString stringWithFormat:
                             @"<html><center><font size=+4 color='red'>The web page is not available. Error description:<br>%@</font></center></html>",
                             error.localizedDescription];
    // Web view:
    [self.view loadHTMLString: errorString baseURL: nil];
}
When you have more than 5 menu items you must deal with the More tab. The off-screen tabs resides inside the “More” tab which is a UINavigationController with the list of off-screen view controllers. iPhone SDK 3.1 UITabBarController class reference is here.