Search code examples
iphoneiosuitabbarcontrollerstoryboarduitabbar

Saving UITabBar settings in StoryBoard


When I create deafault Tab Bar application in Xcode i get Storyboard, AppDelegate and two Controller for default tabs. And what i want to achieve is to save last opened tab after restarting application e.g I'm doing something in the second tab bar and I need to close app. But when i run it again it automatically opens in the second tab.

I saw in 'The iOS 5 Developer's cookbook' that you need to create new method for this (tabBarController:didEndCustomizingViewControllers:changed:) but I have no clue where to put ths as I only have AppDelegate and no Controller for the whole UITabBarController and I'm trying to do this with Storyboard.


Solution

  • UITabBarController *tabbar = [[UITabBarController alloc] init];
    
    // Set selected tab bar Number in UITabBarController Delegate Method
    //    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
    [[NSUserDefaults standardUserDefaults] setInteger:[tabbar selectedIndex] forKey:@"selectedIndex"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    // Retrive the selectedIndex in Method
    //    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    NSInteger selectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedIndex"];
    //Now set your selected Tabbar
    [tabbar setSelectedIndex:selectedIndex];