gpt4 book ai didi

iOS 状态恢复和 UINavigationController 模态视图

转载 作者:行者123 更新时间:2023-12-01 16:43:25 25 4
gpt4 key购买 nike

我正在尝试将状态恢复合并到我的应用程序中。我让它在大多数情况下都可以正常工作,但是在另一个导航 Controller 之上为模态视图呈现导航 Controller 似乎具有挑战性。

为了测试,我在 iPad 上创建了一个新的 Split View应用程序, Split View的两侧都有导航 Controller ,每一侧都有一个主视图 Controller 和一个详细 View Controller ,它们是各自导航 Controller 的根。在主视图中,您可以单击按钮以编程方式将新的 TestViewController 推送到 navController 堆栈上。我在 Storyboard 中连接了 splitView,为所有内容添加了 restoreID,选择加入委托(delegate),提供恢复类并遵守 TestViewController 的 UIViewControllerRestoration 协议(protocol)(因为它是通过编程方式创建的)并且一切正常。如果我关闭应用程序并反驳它,它将启动推送到主导航 Controller 的 TestViewController。到现在为止还挺好。

然后我更改按钮处理程序以在新的 UINavigationController 中显示 TestViewController,将其显示到主导航 Controller 上,以显示模态视图(而不是将其推送到导航堆栈上)。现在,当我重新启动应用程序时,那里不再有模态视图。 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 永远不会保留?不知道为什么,因为它确实有一个 restoreIdentifier。

编辑:

经过进一步测试,事实证明,如果我从 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;
}

编辑 :
附上测试项目链接: dropbox.com/sh/w8herpy2djjl1kw/vw_ZWqimgt

它基本上是 Core Data 主从模板;在 iPad 模拟器上运行它。 Master中的+按钮调用TestModalVC;如果您然后按 Home 按钮,然后终止调试器并再次启动,您会看到快照包含 TestModalVC 但是当应用程序启动时,它不会恢复

最佳答案

您可以创建自己的恢复类来处理此问题,也可以将以下内容添加到您的应用程序委托(delegate)中:

- (UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents
coder:(NSCoder *)coder
{
NSString *lastIdentifier = [identifierComponents lastObject];
if ([lastIdentifier isEqualToString:@"ModalTestID"])
{
UINavigationController *nc = [[UINavigationController alloc] init];
nc.restorationIdentifier = @"ModalTestID";
return nc;
}
else if(...) //Other navigation controllers
{
}

return nil;
}

更多信息请访问 documentation .

关于iOS 状态恢复和 UINavigationController 模态视图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22217333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com