UIViewControllers正在创建但未在状态还原中显示

问题描述:

我有一个应用程序无法正常工作状态还原。它以前做过,但是当我开始离开故事板时,它停止了。UIViewControllers正在创建但未在状态还原中显示

我的应用程序以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:为我的LoginViewController被调用。这并没有太大的作用,它的使用是可选的。我尝试删除它,我没有任何不良影响。

+ (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; 
} 

接下来,我FolderViewControllerviewControllerWithRestorationIdentifierPath:coder:被称为:

// 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没有被显示。在我的LoginViewController中是否有缺少的设置需要附加我以前手动添加的视图控制器?

编辑

阅读http://aplus.rs/2013/state-restoration-for-modal-view-controllers/这似乎有关,我下面的代码添加到应用程序委托:

- (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; 
} 

我认为应用是现在过得快乐,但它仍然没有恢复正常。现在,我得到这个错误在我的日志:

警告:试图提出<的UINavigationController:0xbaacf50 >上< LoginViewController:0xbaa1260 >谁的观点是不是在窗口层次!

这是不同的。

+0

你有没有为此做一些解决方法。我有类似的问题,我有一个自定义选项卡,其中添加childviewcontrollers包裹在导航控制器,当我打印它给出正确的路径,但不恢复到推控制器,虽然它保存到自定义选项卡,通过编码索引和childviewcontroller ... – Sabby

您需要为不同的FolderViewController对象分配不同的恢复标识符。

例如:

FolderViewController *folderViewController1 = // initialize object ; 
FolderViewController *folderViewController2 = // initialize object ; 

folderViewController1. restorationIdentifier = @"folderViewController1"; 
folderViewController2. restorationIdentifier = @"folderViewController2"; 

我想上面的代码,它工作得很好。