【发布时间】:2019-08-28 06:20:58
【问题描述】:
我有一个库,其中包含实现 iOS 状态保存的情节提要和控制器类。
要从主应用程序的委托启动库,我使用以下内容:
- (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”情节提要的库的视图控制器。
在主应用的委托中,我还添加了 shouldSaveApplicationState 和 shouldRestoreApplicationState,它们都返回 YES。
当我的应用程序进入后台时,iOS 在委托中正确调用 shouldSaveApplicationState 并继续调用库控制器的 encodeRestorableStateWithCoder 方法。
但是,当它尝试恢复时,iOS 正确调用了主应用委托的 shouldRestoreApplicationState 方法,但随后立即崩溃并出现以下异常:
Exception occurred restoring state Could not find a storyboard named 'libraryMain' in bundle ... Main App.app
所以 iOS 正在主应用程序包中寻找 libraryMain 故事板。如何让 iOS 查看库的捆绑包?还是无法在 iOS 库中实现状态恢复?
谢谢!
【问题讨论】:
-
this 是您问题的解决方案吗?
-
谢谢。但是,不确定如何使用它。我是否在 shouldRestoreApplicationState 方法期间将当前包更改为我的库?这看起来很 hacky,如果主应用程序也有需要恢复的故事板会发生什么?现在我正在寻找stackoverflow.com/questions/26077127/… 寻找可能的解决方案。
-
使用 [NSBundle mainBundle] 而不是 [NSBundle bundleForClass:self.class]
标签: ios state-restoration ios-library