我想在我的应用程序中加入State Restoration . 我在大多数情况下都能正常工作,但在另一个导航控制器上呈现模态视图的导航控制器似乎具有挑战性 .
为了测试,我在iPad上创建了一个新的拆分视图应用程序,分割视图两侧都有导航控制器,每侧都有一个Master和Detail视图控制器,它们各自的navcontrollers的根 . 在主视图中,您可以单击按钮以编程方式将新的TestViewController推送到navController堆栈 . 我在故事板中连接了splitView,向所有内容添加了restoreID,选择了委托,提供了一个恢复类并遵守TestViewController的UIViewControllerRestoration协议(因为它是以编程方式创建的),一切正常 . 如果我关闭应用程序并反击它,它将启动TestViewController推送到主控的navcontroller . 到现在为止还挺好 .
然后我更改按钮处理程序以在新的UINavigationController中显示TestViewController,将其呈现在主导航控制器上,以显示模态视图(而不是在导航堆栈上推送它) . 现在,当我重新启动应用程序时,再也没有模态视图了 . TestModalViewController的viewControllerWithRestorationIdentifierPath:coder:实际上和以前一样被正确调用,但是模态视图从未出现过某种原因 .
这是我正在谈论的代码
MasterViewController.h:
- (void)pushButton:(id)sender
{
TestModalViewController *test = [[TestModalViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
test.restorationIdentifier = @"testid";
test.restorationClass = [TestModalViewController class];
UINavigationController *modal = [[UINavigationController alloc] initWithRootViewController:test];
modal.modalPresentationStyle = UIModalPresentationFormSheet;
modal.restorationIdentifier = @"ModalTestID";
[self.navigationController presentViewController:modal animated:YES completion:nil];
return;
}
TestModalViewController.m:
+ (UIViewController *) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder {
TestModalViewController *test = [[TestModalViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
test.restorationClass = [TestModalViewController class];
test.restorationIdentifier = [identifierComponents lastObject];
return test;
}
也许永远不会保留为模态显示而创建的UINavigationController?不知道为什么,因为它确实有一个restorationIdentifier .
Edit:
经过进一步测试后,如果我从pushButton:代码中删除UINavigationController,并直接呈现TestModalViewController实例,它就会正确恢复 . 那么从另一个UINavigationController呈现的UINavigationController呢?
这有效(虽然不是我真正想要的):
- (void)pushButton:(id)sender
{
TestModalViewController *test = [[TestModalViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
test.restorationIdentifier = @"testid";
test.restorationClass = [TestModalViewController class];
//UINavigationController *modal = [[UINavigationController alloc] initWithRootViewController:test];
//modal.modalPresentationStyle = UIModalPresentationFormSheet;
//modal.restorationIdentifier = @"ModalTestID";
[self.navigationController presentViewController:test animated:YES completion:nil];
return;
}
EDIT :测试项目的附加链接:dropbox.com/sh/w8herpy2djjl1kw/vw_ZWqimgt
它基本上是核心数据主要细节模板;在iPad模拟器上运行它 . Master中的按钮调用TestModalVC;如果您然后按Home键,然后终止调试器并再次启动,您会看到快照包含TestModalVC,但是当应用程序启动时,它不会恢复
1 回答
您可以创建自己的恢复类来处理此问题,也可以将以下内容添加到您的应用代理中:
documentation中的更多信息 .