IPhone Dev – Lazy loading a Tab Bar app

iphonelazy-loadinguitabbarcontrolleruiviewcontroller

How can I make it so when a tab is selected, the current one is unloaded, and the next one is loaded so only one loaded at a time? Or should I not even do this? I know how to do it with a normal UIViewController as the root VC, but not sure with a UITabBarController. Also, is there a way to animate the transition from one tab to the next? Any help? Thanks!!

EDIT: … If I unload the view controllers, then their icons on the tab bar are gone… maybe I'll just unload their views..

Best Answer

I can answer both questions in one...

You just need a class that acts as the UITabBarController delegate, then implement a method like so:

// Animate tab selections so they fade in and fade out
-(void)tabBarController:(UITabBarController*)tbc didSelectViewController:(UIViewController*)newSelection
{
    [UIView beginAnimations:@"TabFadeIn" context:nil];
    [UIView setAnimationDuration:0.6];
    for( UIViewController* vc in tbc.viewControllers )
        vc.view.alpha = (vc==newSelection) ? 1 : 0;
    [UIView commitAnimations];  
}

Now my code simply makes the tab bars fade in and out, but you could also do work here to unload non-used tabs. Sometimes that is a good idea if some of the tabs will be using a ton of memory.