1

I am using iOS 6 Preservation and Restoration (without Storyboard), it is working fine with navigation controller , but if i manually add Tabbar controller on main window , i am not getting selected tab.

eg.

  ListViewController *list = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil];
 SettingViewController *setting = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];

 UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:list];
 navigation.restorationIdentifier = @"NavigationControllerID";

 self.tabbar = [[UITabBarController alloc] init];
 self.tabbar.restorationIdentifier = @"TabbarControllerID";
    self.tabbar.viewControllers = @[navigation,setting];

 [[_tabbar.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(@"List", @"comment")];
 [[_tabbar.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"Setting", @"comment")];

 self.window.rootViewController = self.tabbar;
 [self.window makeKeyAndVisible];

in t his case i am getting first tab selected every time.i have implatementd

+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder

for setting view controller.

PJR
  • 13,052
  • 13
  • 64
  • 104

1 Answers1

0

By Adding this methods in Appdelegate

NSString * const AppDelegateRootVCKey = @"AppDelegateRootVCKey";

 - (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
//Adding last tabbar selected index
[coder encodeObject:[NSString stringWithFormat:@"%d",self.tabbar.selectedIndex]forKey:AppDelegateRootVCKey];

}

   - (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {


//Setting Last tabbar selected index
NSString *selectedStr = [coder decodeObjectForKey:AppDelegateRootVCKey];
self.tabbar.selectedIndex = [selectedStr intValue];

return YES;

}

PJR
  • 13,052
  • 13
  • 64
  • 104