目标:我正在尝试在基于选项卡 Controller 的应用程序上恢复状态(每个选项卡上都有导航 Controller )。
问题:重新启动时,所选选项卡似乎已按预期恢复,但该选项卡内的导航层次结构却没有。
开发:
application(_:shouldSaveApplicationState:)
和 application(_:shouldRestoreApplicationState:)
. 我运行然后应用程序,切换到第二个(右)选项卡,点击主页,终止。 o 重新启动,显示右侧选项卡(如预期)。 到现在为止还挺好。
我运行该应用程序,恢复仍然有效。 还好。
UIViewController
的自定义子类到 Storyboard ,具有配置调试标签内容及其 View 背景颜色的属性。 prepareForSegue(_:sender:)
上方法,我以不同的方式配置推送的详细信息 View Controller :不同的文本和背景颜色(分别为“左”和蓝色,“右”和红色)。 encodeRestorableStateWithCoder(_:)
和 decodeRestorableStateWithCoder(_:)
.另外,我实现了 viewDidLoad()
以便在 View 中反射(reflect)这些属性的值。每当它被实例化并通过 segue 推送到导航时,首先设置属性,然后用于配置 viewDidLoad()
中的 View 。 .每当它在恢复期间实例化时,属性都设置在 decodeRestorableStateWithCoder(_:)
中。并类似地用于 viewDidLoad()
. ...但是当我运行这段代码时,最后选择的选项卡已恢复,但仅限于顶 View Controller -左或右-,不是细节 .有趣的是,最后设置到细节 View Controller 的背景颜色会闪烁一瞬间。
我在
encodeRestorableStateWithCoder(_:)
中放置了断点和 decodeRestorableStateWithCoder(_:)
,但只有第一个被执行(编码)。application(_:viewControllerWithRestorationIdentifierPath:coder:)
(始终返回 nil,但记录传递的路径组件)。 文档对是否需要这种方法不是很清楚,无论如何,即使没有它,除了细节之外的所有 View Controller 似乎都可以完美地恢复。我添加了代码以根据最后一个路径组件(即该 Controller 的恢复 ID)实例化每个 View Controller 并返回它。
现在,
decodeRestorableStateWithCoder(_:)
被调用,但导航仍会在一瞬间返回到选项卡的顶 View Controller 。那么发生了什么?在 Tab Bar + Navigation Controller 应用程序中实现状态保存和恢复我缺少什么?
最佳答案
固定:所以,我的代码有几个问题......
application(_:viewControllerWithRestorationIdentifierPath:coder:)
. (见 comments of this answer)encodeRestorableStateWithCoder(_:)
的实现和 decodeRestorableStateWithCoder(_:)
没有打电话 super
(如上述问题的 accepted answer 所建议的那样)。 decodeRestorableStateWithCoder(_:)
之后立即调用(就像我假设的那样),所以我从 viewDidLoad()
调用了一个通用方法和 decodeRestorableStateWithCoder(_:)
更新用户界面。 像往常一样,我在搜索或尝试对我的代码进行足够的修改之前急于发布一个问题(我的歉意......)。
我希望这至少可以帮助其他人。
像往常一样,我会等几天再接受我自己的答案,以防有人透露更多信息。
关于ios - 标签栏和导航 Controller 应用程序中的状态恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32471513/