1

我正在使用 iOS 6 Preservation and Restoration(没有 Storyboard),它与导航控制器一起工作正常,但是如果我在主窗口上手动添加 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];

在他的情况下,我每次都会选择第一个选项卡。我已经植入

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

用于设置视图控制器。

4

1 回答 1

0

通过在 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;

}

于 2013-10-22T11:37:07.623 回答