【问题标题】:Preservation and Restoration for Tabbar without Storyboard没有 Storyboard 的 Tabbar 的保存和恢复
【发布时间】:2023-03-31 16:05:01
【问题描述】:

我正在使用 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

用于设置视图控制器。

【问题讨论】:

    标签: iphone ios objective-c ios6 uikit-state-preservation


    【解决方案1】:

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

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多