我有一个带有 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 :
- viewControllerWithRestorationIdentifierPath:coder:恢复类(firstController 类,在你的问题中)。在这里您可以创建并配置第一个 Controller 的实例
- application:viewControllerWithRestorationIdentifierPath:coder:应用程序委托(delegate)的。这里可以根据恢复路径创建类的实例。
上面的两个选项看起来都像是解决方法,因为我没有实际的项目设置来重现问题。
关于ios - iOS 库中可以恢复 iOS 状态吗? -- 找不到名为的 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55550755/