如何跳转到故事板中的 UIViewController 并仍然保留导航层次结构?

发布于 2025-01-08 10:16:01 字数 437 浏览 0 评论 0原文

我正在为 ios5 开发一个基于故事板的项目,到目前为止,它运行良好,但我想在用户退出时保存用户的状态,以便当他们返回应用程序时,我可以将它们跳回到其中的位置他们离开时所在的故事板。

我的理解是,当您浏览故事板时,它会创建一堆 UIViewController,以便当您关闭当前控制器时,它知道要显示哪个。但是,如果我在应用程序启动时以编程方式跳转到情节提要中的 UIViewController 之一,那么它就不会具有可回溯的历史记录、控制器堆栈。如果不出意外,如果两个控制器在情节提要中的某个时刻连接到同一个控制器,它将不知道要返回到哪个。

处理这个问题的正确方法是什么?我是否只需浏览故事板到用户停止的位置,将每个 UIViewController 的动画设置为“否”,直到到达用户所在的位置? (如果是这样,存储该信息的最佳方法是什么?是否有任何方法/类可以帮助实现这一点,或者我是否采用自己的方式通过故事板存储它们?)

I'm working on a storyboard-based project for ios5, and it is working well so far, but I want to save the user's state when they exit so that when they return to the app, I can jump them back to the place in the storyboard where they were when they left.

My understanding is that as you navigate through a storyboard, it creates a stack of UIViewControllers, so that when you dismiss the current controller, it knows which one to display. But if I jump to one of the UIViewControllers in a storyboard programmatically when the app starts up, it would not have that history, that stack of controllers, to backtrack along. If nothing else, if two controllers segue into the same controller at some point in the storyboard, it wouldn't know which to go back to.

What is the proper way of dealing with this? Do I just go through the storyboard to the point where the user left off, presenting each UIViewController with animations set to NO until I get to the one that the user was on? (And if so, what's the best way to store that information? Are there any methods/classes that can assist with this, or am I rolling my own way of storing their through the storyboard?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

善良天后 2025-01-15 10:16:01

一种选择是使用 NSUserDefaults< /a>.您无法将 UIViewControllers 的确切实例准确地保存在磁盘上,并像在内存中一样检索它们。发生的情况是,您应该以某种方式(例如键/值)识别您感兴趣并想要保存其状态的每个视图控制器。此外,您还需要为要保存其状态的每个元素设置键,然后使用 NSUserDefaults 进行保存。

例如,假设您有这样的层次结构:

HomeView >注释列表> NoteAtIndexPath:x

用户正在查看 NoteAtIndexPath:x,并且我们假设字体大小是动态的,因此用户可以在查看时将其放大或缩小。您可以定义两个键:IndexOfLastNoteViewedLasFontSizeViewed,并保存它们:

 [[NSUserDefaults standardUser] setInteger:5 forKey:IndexOfLastNoteViewed];
 [[NSUserDefaults standardUser] setFloat:17.0 forKey:LasFontSizeViewed];

当应用程序再次启动时,您可以检查这些键,并以编程方式浏览到 HomeView > 。注释列表>注意索引路径:5。

以下是一些可能对您有帮助的链接:

One option is using NSUserDefaults. You can't exactly save the exact instances of UIViewControllers on the disk, and retrieve them as if they were in the memory. What happens is that, you should somehow, e.g. key/value, identify each of your viewControllers that you are interested and want to save their state. Also you need to key each element you want to save its state, and then save using NSUserDefaults.

For exmaple, let's say you have a hierarchy like this:

HomeView > ListOfNotes > NoteAtIndexPath:x

And the user is viewing NoteAtIndexPath:x, and also let's assume the font size is dynamic, so user can make it bigger or smaller at the time of viewing. You can define two keys: IndexOfLastNoteViewed and LasFontSizeViewed, and save them:

 [[NSUserDefaults standardUser] setInteger:5 forKey:IndexOfLastNoteViewed];
 [[NSUserDefaults standardUser] setFloat:17.0 forKey:LasFontSizeViewed];

When the application launches again, you can check the keys, and browse programmatically to HomeView > ListOfNotes > NoteAtIndexPath:5.

Here are some links that might help you:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文