在 NSViewController 之间切换

发布于 2024-11-13 19:51:30 字数 381 浏览 2 评论 0原文

我正在开发一个 Mac 应用程序。该应用程序的左侧有一个公共源视图,右侧有一个详细视图,这是整个窗口的主要部分。

这就像主从关系,但源视图中的每个元素都需要另一个详细视图。事实上,我为源视图中的每个元素设计了一个特定的 NSViewController。

如果我在这些 NSViewController 之间切换,这意味着如果我在源视图中选择另一个元素,我将删除当前视图并添加新选择的 NSViewController 的视图。每次我更改 NSViewController 时,它的状态都会丢失。当用户回到 NSViewController 时,他必须重新开始。

我现在的问题是:如何保存 NSViewController 的状态,以便我可以在它们之间切换而不丢失其状态,并且可以从我离开的地方继续?

I'm developing a Mac Application. The application has a common source view on the left and a detail view on the right which is the main part of the whole window.

It's like a Master-Detail relationship, but each element in the source view require another detail view. In fact, I have designed a specific NSViewController for each element in the source view.

If I'm switching between these NSViewControllers, that means If I select another element in the source view, I remove the current view and add the view of the newly selected NSViewController. Everytime I change the NSViewController, its state will be lost. When the user comes back to that NSViewController, he has to start over.

My question now is: How can I save the state of the NSViewController, so that I can switch between these without losing its states and can continue where I have left?

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

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

发布评论

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

评论(2

枫以 2024-11-20 19:51:30

关于您的问题的两个注意事项:

  • 将模型数据保留在模型类中。这意味着您始终可以重新创建视图控制器并设置其表示的对象,前提是模型类保留了通过视图控制器所做的更改。当您需要实例化视图控制器时,请将其表示的对象设置为模型类(的表示)。

  • 从其父视图中删除视图时,不一定需要释放其相应的视图控制器。相反,您可以在窗口控制器/应用程序委托中保留对所有视图控制器的强引用,因此实际上不会丢失任何状态。

Two considerations about your problem:

  • Keep model data in model classes. This means that you can always recreate a view controller and set its represented object provided the model classes have kept the changes made via the view controller. When you need to instantiate a view controller, set its represented object to (a representation of) a model class.

  • When removing a view from its superview, you do not necessarily need to release its corresponding view controller. Instead, you can keep strong references to all view controllers in your window controller/application delegate, so no state is actually lost.

温柔嚣张 2024-11-20 19:51:30

使用 NSArchiver。在 dealloc/init 方法中实现归档/取消归档,并将每个视图控制器的状态存储在以类命名的文件中(如果每个视图控制器策略有一个项目)。否则,考虑一些简单的命名约定并使用它。

Use NSArchiver. Implement archiving/unarchiving in your dealloc/init methods and store each view controller's state in a file named after the class (if you have one item per view controller policy). Otherwise think of some simple naming convention and use it.

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