我有一个应用程序在状态恢复时无法正常工作。以前是这样,但是当我开始远离故事板时,它停止了。
我的应用程序以LoginViewController
我的故事板中的起始视图控制器开头。如果登录成功,则尝试将两个添加FolderViewController
到导航控制器。这样可见文件夹已经深了一层。这是在以下代码中完成的:
UINavigationController *foldersController = [[UINavigationController alloc] initWithNavigationBarClass:nil toolbarClass:nil];
foldersController.restorationIdentifier = @"FolderNavigationController";
FolderViewController *root = [storyboard instantiateViewControllerWithIdentifier:@"FolderView"];
root.folderId = 0;
FolderViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:@"FolderView"];
fvc.folderId = 1;
[foldersController setViewControllers:@[root, fvc] animated:YES];
[self presentViewController:foldersController animated:YES completion:nil];
FolderViewController
有这个awakeFromNib
- (void)awakeFromNib
{
[super awakeFromNib];
self.restorationClass = [self class]; // If we don't have this, then viewControllerWithRestorationIdentifierPath won't be called.
}
在故事板中,FolderViewController 有一个restorationIdentifier
集合。当我按下主页按钮时,应用程序被暂停。我的恢复电话FolderViewController
正在被调用:
// This is being called
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[super encodeRestorableStateWithCoder:coder];
[coder encodeInt64:self.folderId forKey:@"folderId"];
}
现在的问题是当我尝试恢复时。我在调试器中停止应用程序,然后重新启动它。这将启动恢复过程。
首先,我viewControllerWithRestorationIdentifierPath:coder:
的 for myLoginViewController
被称为。这并没有多大作用,它的使用是可选的。我试过删除它,我没有任何不良影响。
+ (UIViewController*) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
LoginViewController* vc;
UIStoryboard* sb = [coder decodeObjectForKey:UIStateRestorationViewControllerStoryboardKey];
if (sb)
{
vc = (LoginViewController *)[sb instantiateViewControllerWithIdentifier:@"LoginViewController"];
vc.restorationIdentifier = [identifierComponents lastObject];
vc.restorationClass = [LoginViewController class];
}
return vc;
}
接下来,viewControllerWithRestorationIdentifierPath:coder:
for myFolderViewController
被称为:
// This is being called
+ (UIViewController*) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
FolderViewController* vc;
UIStoryboard* sb = [coder decodeObjectForKey:UIStateRestorationViewControllerStoryboardKey];
if (sb)
{
vc = (FolderViewController *)[sb instantiateViewControllerWithIdentifier:@"FolderView"];
vc.restorationIdentifier = [identifierComponents lastObject];
vc.restorationClass = [FolderViewController class];
vc.folderId = [coder decodeInt32ForKey:@"folderId"];
}
return vc;
}
我以前也有一个decodeRestorableStateWithCoder:
,它确实被调用了。但是,由于它是在 中设置的viewControllerWithRestorationIdentifierPath:coder:
,因此没有必要保留它。
所有这些事情都被调用了适当的次数。但最终,唯一显示在LoginViewController
. 为什么我FolderViewController
的 s 不显示。我需要在我LoginViewController
的附加视图控制器中做一些缺失的设置吗?我之前手动添加的?
编辑
在阅读了似乎相关的http://aplus.rs/2013/state-restoration-for-modal-view-controllers/之后,我在 App 委托中添加了以下代码:
- (UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
if ([identifierComponents.lastObject isEqualToString:@"FolderNavigationController"])
{
UINavigationController *nc = [[UINavigationController alloc] init];
nc.restorationIdentifier = @"FolderNavigationController";
return nc;
}
else
return nil;
}
我认为应用程序现在更快乐,但它仍然没有正确恢复。现在我在我的日志中得到这个错误:
警告:尝试在视图不在窗口层次结构中的 <LoginViewController: 0xbaa1260> 上呈现 <UINavigationController: 0xbaacf50>!
这是不同的东西。