保存和恢复为Tabbar没有故事板
问题描述:
我使用iOS 6保存和恢复(没有故事板),它与导航控制器工作正常,但如果我手动添加Tabbar控制器在主窗口,我没有得到选择选项卡。保存和恢复为Tabbar没有故事板
例如。
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];
在吨
他的情况下,我得到选择的每个再寄一次第一选项卡已经implatementd
+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
用于设置视图控制器。
答
通过的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;
}