iOS库中是否可以恢复iOS状态? -找不到名为的情节提要 [英] Is iOS state restoration possible in iOS library? -- Could not find a storyboard named

查看:93
本文介绍了iOS库中是否可以恢复iOS状态? -找不到名为的情节提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含情节提要和实现iOS状态保存的控制器类的库.

I have a library with a storyboard and controller classes that implement iOS state preservation.

要从主应用程序的代理启动库,请使用以下命令:

To launch the library from the main app's delegate, I use the following:

- (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:

Then inside my library, firstController is created with:

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

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

到目前为止,一切都很好.它会启动使用库的"libraryMain"情节提要的库的视图控制器.

So far so good. It starts the library's view controller that uses the library's "libraryMain" storyboard.

在主应用程序的委托中,我还添加了shouldSaveApplicationState和ShouldRestoreApplicationState,两者均返回YES.

In the main app's delegate, I've also added shouldSaveApplicationState and shouldRestoreApplicationState, both of which return YES.

当我的应用程序进入后台时,iOS会正确调用委托中的shouldSaveApplicationState并继续调用库的控制器的encodeRestorableStateWithCoder方法.

When my app goes to the background, iOS correctly calls shouldSaveApplicationState in delegate and proceeds to call the library's controller's encodeRestorableStateWithCoder methods.

但是,当它尝试还原时,iOS会正确调用主应用程序委托的shouldRestoreApplicationState方法,但随后立即崩溃,并出现以下异常:

However, when it tries to restore, iOS correctly calls the main app delegate's shouldRestoreApplicationState method, but then immediately crashes with the following exception:

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

因此,iOS正在主应用程序包中查找libraryMain故事板.如何让iOS出现在库的捆绑包中?还是在iOS库中无法实现状态恢复?

So iOS is looking for the libraryMain storyboard in the main app's bundle. How do I get iOS to look in the library's bundle? Or is it just not possible to implement state restoration in an iOS library?

谢谢!

推荐答案

如果'libraryMain'是链接到主应用程序的静态库,则它不包含情节提要文件,并且iOS缺少文件放在主捆绑包中(除非您专门提供).

If the 'libraryMain' is a static library that you link to the main app, then it doesn't contain the storyboard files, and the iOS is missing the file in the main bundle (unless you specifically provide it).

原因是静态库是已编译代码的存档,并且资源必须单独包含.在这种情况下,您需要找到一种方法来捆绑您的资源-将'libraryMain'故事板直接添加到主应用程序中,或创建一个"resource"捆绑包.

The reason is that static libraries are archives of the compiled code, and the resources have to be included separately. In this case, you need to find a way to bundle your resources - either including the 'libraryMain' storyboard directly into the main app or creating a "resource" bundle.

如果"libraryMain"是一个内部装有情节提要文件的框架,那么有一些解决方法.

If the 'libraryMain' is a framework with a storyboard file inside, then there are some workarounds.

关于状态保存说明的文档 iOS检查两个位置以还原控制器的地方:

The documentation about the state preservation notes that there are two places that iOS checks in order to restore the controller:

  1. viewControllerWithRestorationIdentifierPath:coder:类(在您的问题中为firstController类).在这里您可以创建和配置第一个控制器的实例
  2. application:viewControllerWithRestorationIdentifierPath:coder:应用程序委托.在这里,您可以基于还原路径创建该类的实例.
  1. viewControllerWithRestorationIdentifierPath:coder: of the restoration class (firstController class, in your question). Here you can create and configure the instance of the first controller
  2. application:viewControllerWithRestorationIdentifierPath:coder: of the app delegate. Here you can create the instance of the class based on the restoration path.

以上两个选项都看起来像变通办法,因为我没有您的项目的实际设置来重现该问题.

Both of the options above look like workarounds as I don't have the actual setup of your project to reproduce the problem.

这篇关于iOS库中是否可以恢复iOS状态? -找不到名为的情节提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆