views:

335

answers:

2

I'm trying to restore the state of my multi-tabbed iPhone application. There are more than 5 tabs, each with it's own navigation controller. On applicationDidFinishLaunching, I determine which was the last tab the user was on and set it with

myTabController.selectedIndex = persistedTabIndex;

I then call a function on that tab's root view controller to restore itself. The problem is, if the tab has been moved to the "More" page, the view has not been loaded and the call disappears into NIL land. Is there a way to force the view controller in question to load it's view?

Many thanks in advance for your consideration and responses.

A: 

That's odd; if the navigation controller is in the tab controller's array, it should be getting its -loadView method called as usual when it's selected, regardless of whether it's in the main 4. You might try manually calling [thePersistedController loadView] after creating it but before telling the tab controller to select it.

Noah Witherspoon
+1  A: 

I'm using some code I found on the web to save the last loaded tab. In my app delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{ 
 // .. my app set up is here

 // Select the tab that was selected on last shutdown
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 NSInteger whichTab = [defaults integerForKey:kSelectedTabDefaultsKey];
 self.tabBarController.selectedIndex = whichTab;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
 // Save the current tab so the user can start up again in the same place.
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 NSInteger whichTab = self.tabBarController.selectedIndex;
    [defaults setInteger:whichTab forKey:kSelectedTabDefaultsKey];
}

and the definition in my interface file:

#define kSelectedTabDefaultsKey @"SelectedTab"

This works unless the user rearranges the tabs, in which case you have to update the array of tabs (the index will change).

Here's the original page where I found the code:

http://iphonedevelopment.blogspot.com/2009/09/saving-tabs.html

I'm using this code on a tabbed interface which shows the "More..." tab. When I quit on a tab under the "More..." part, the interface comes back to that tab when I restart the app. The interface won't restart on the "More..." table view, but I don't consider that a problem.

nevan