UITabBarController 的 iOS 状态恢复不恢复 subview Controller 或选定索引

标签 ios swift uitabbarcontroller state tabbarcontroller

(第一次在StackOverflow上提问,有问题请多多包涵)

我的问题归结为无法正确恢复 UITabBarController 的状态。这是一个基于选项卡的应用程序,我没有使用 Storyboard。

我选择了状态恢复。我在它们各自的 init 方法中为我的所有自定义 ViewController 提供了唯一的恢复标识符和恢复类。

至于UITabBarController,我在创建时为其提供了一个唯一的恢复标识符。因为我没有给它一个恢复类,所以我在app delegate中实现了如下代码:

- (UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder 
{
    UITabBarController *vc = [[UITabBarController alloc] init];

    vc.restorationIdentifier = [identifierComponents lastObject];

    if ([identifierComponents count] == 1) {
        self.window.rootViewController = vc;
    }

    return vc;
}

我在其他应用中用同样的方法恢复了NavigationController的状态,一切顺利。但是,当我使用 TabBarController 为这个应用程序执行此操作时,应用程序以空的 TabBarController 启动(TabBarController 已恢复,但它确实不恢复它的 subview Controller )。

这对我来说并不是一个巨大的冲击,我想只是本质上 UITabBarController 不会保留对其 subview Controller 的引用,而不是用于状态恢复的选定选项卡索引。然而,尽管在线搜索了很多小时,但我无法找到任何关于如何保存 TabBarController 状态的说明(没有 storyboard)。非常感谢任何有关如何执行此操作的帮助。

再次,如果我没有提供足够的细节或不够清楚,我深表歉意。请让我知道您在回答中需要什么,我会提供。我知道我没有提供太多代码,但我真的只需要有关如何在没有 Storyboard的情况下保留基于选项卡的应用程序状态的说明。它不需要针对我的具体情况进行定制;我将能够自己应用一般流程。

谢谢!

最佳答案

您需要对每个 child 的 View Controller 进行编码。 但 iOS 文档声明您不对这些进行解码 - 该位将自动发生。

override func encodeRestorableStateWithCoder(coder: NSCoder) {
    coder.encodeObject(child0VC, forKey:"child0VC")
    coder.encodeObject(child1VC, forKey:"child1VC")
    super.encodeRestorableStateWithCoder(coder)
}

关于UITabBarController 的 iOS 状态恢复不恢复 subview Controller 或选定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31555365/

相关文章:

ios - 如何保存文本数据

swift - 如何对私有(private) Cocoapods 库进行单元测试?

iphone - 自定义 UITabBarController 的 moreNavigationController

iPhone:如何在 Tabbar 应用程序中的多个 View Controller 之间传递数据

ios - 无法在 App 中将 json 字符串插入到 sqlite

ios - 如何区分 CoreData 中的 Boolean 和 NSNumber

ios - 如何编写自定义 UILabel 类

Swift 1.2 在初始化后分配 let

swift - 如何将数据从 UIViewController 传递到 UITabBarController?

ios - SSKeychain:帐户未存储在 iCloud 中?