ios - iOS 库中可以恢复 iOS 状态吗? -- 找不到名为的 Storyboard

标签 ios state-restoration ios-library

我有一个带有 Storyboard和 Controller 类的库,用于实现 iOS 状态保存。

要从主应用程序的委托(delegate)启动库,我使用以下命令:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = myLibrary.sharedInstance.firstController;

    return YES;
}

然后在我的库中,firstController 是用以下命令创建的:

- ( UIViewController * _Nullable ) firstController
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"libraryMain"
        bundle:[NSBundle bundleForClass:self.class]];

    return [storyboard instantiateViewControllerWithIdentifier:@"firstController"];
}

到目前为止一切顺利。它启动使用库的“libraryMain” Storyboard的库 View Controller 。

在主应用程序的委托(delegate)中,我还添加了shouldSaveApplicationState和shouldRestoreApplicationState,两者都返回YES。

当我的应用程序转到后台时,iOS 会正确调用委托(delegate)中的shouldSaveApplicationState,并继续调用库的 Controller 的encodeRestorableStateWithCoder 方法。

但是,当它尝试恢复时,iOS 正确地调用了主应用程序委托(delegate)的 shouldRestoreApplicationState 方法,但随后立即崩溃并出现以下异常:

Exception occurred restoring state Could not find a storyboard named 'libraryMain' in bundle ... Main App.app

因此,iOS 正在主应用程序包中寻找库Main Storyboard。如何让 iOS 查看库的 bundle ?或者只是不可能在 iOS 库中实现状态恢复?

谢谢!

最佳答案

如果“libraryMain”是链接到主应用程序的静态库,则它不包含 Storyboard文件,并且 iOS 缺少主包中的文件(除非您专门提供)。

原因是静态库是编译代码的存档,必须单独包含资源。在这种情况下,您需要找到一种方法来捆绑资源 - 要么将“libraryMain” Storyboard直接包含到主应用程序中,要么创建“资源” bundle 。

如果“libraryMain”是一个内部有 Storyboard文件的框架,那么有一些解决方法。

documentation关于状态保存,请注意,iOS 检查两个位置以恢复 Controller :

  1. viewControllerWithRestorationIdentifierPath:coder:恢复类(firstController 类,在你的问题中)。在这里您可以创建并配置第一个 Controller 的实例
  2. application:viewControllerWithRestorationIdentifierPath:coder:应用程序委托(delegate)的。这里可以根据恢复路径创建类的实例。

上面的两个选项看起来都像是解决方法,因为我没有实际的项目设置来重现问题。

关于ios - iOS 库中可以恢复 iOS 状态吗? -- 找不到名为的 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55550755/

相关文章:

ios - 在 Swift 项目中使用以 Objective-c 编写的库类

ios - 为什么我需要在 drawInRect 中调用 glClear?

ios - 为什么 Spotify iOS SDK 在 SPTAudioStreamingController.login() 上无限期挂起?

ios - 为 UIColor 实现 Codable

ios - MPMoviePlayerController 切换视频

ios - iOS 8 中的 UISplitViewController 状态恢复

ios - 每次都恢复状态?

ios - UINavigationController 状态恢复(无 Storyboard)

ios - 如何为 Objective-C 和 Swift 创建一个通用的 iOS 库?