我应该为此使用 NSViewController 吗?
我希望我的应用程序有一个文档,但有多个屏幕,而不需要求助于多个窗口。实现这一目标的最佳方法是什么?我正在考虑使用一个 NSPersistentDocument ,它将从显示一组控件的 NSViewController 开始,然后将该视图交换为另一个 NSViewController 。
我的问题是:这是 NSViewControllers 的正确用法吗?还可以做到吗?如果两者的答案都是肯定的,我如何在文档中将一个视图交换为另一个视图?
I want my application to have a single document but several screens without resorting to several windows. What's the best way to achieve this? I'm thinking of using a single NSPersistentDocument that will start with a NSViewController displaying a set of controls and then swap that view for another NSViewController.
My question is: is this the correct use of NSViewControllers? Can it even be done? And if the answer to both is yes, how do I swap a view for another in a document?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是完全可以做到的。是的,您应该使用视图控制器,但不是必须的。这严格来说是一种风格选择。如果您不使用视图控制器,那么所有控制器逻辑可能都在您的单个窗口控制器中。对于大型应用程序来说,这可能会很痛苦。视图控制器可帮助您将 UI 分解为逻辑组件和控制单元。
您可以像这样执行视图控制器方法:
现在您已经获得了具有多个顶级视图控制器的应用程序的基本框架。从这里开始,任务是根据需要或用户输入的指示在主窗口中排列视图。有时您可能有 3-4 个顶级视图,有时有 1 个,在您的情况下,您只想将一个视图替换为另一个视图。这些场景之间没有太大区别。
有很多方法可以实现这一目标。
对于您的具体情况,一种简单的方法是:
一般来说,您可以做很多事情:
顺便说一句,无论您是否正在创建基于文档的应用程序,也无论它是基于单窗口还是多窗口文档的应用程序,此方法都有效。
使用视图控制器的一个缺点是许多顶级视图布局和管理必须以编程方式完成,而不是在 IB 中完成。但这并不那么困难。
通常你只需要执行 addSubview: 和 setFrame: ,将你的视图放置在你已经在 IB 中布局的父视图中。
但要做更复杂和手动的事情,您应该阅读文档并了解以下工作原理:框架/边界、翻转坐标、自动调整大小以及如何覆盖自动调整大小以执行您自己的布局。
Yes, it's completely possible to do. Yes, you should use view controllers but you don't have to. That's strictly a stylistic choice. If you don't use a view controller then all the controller logic would likely be in your single window controller. That might get painful for a big app. View controllers help you break down your UI into logical components and control units.
You'd go about doing the view controller method like this:
Now you've got the basic framework for an app with multiple top-level view controllers. From here the task is to arrange the views in the main window as you need them or as user input dictates. Sometimes you might have 3-4 top-level views sometimes 1, and in your case you just want to swap out one for the another. There is no huge difference between these scenarios.
There are many ways to achieve this.
For your specific case, an easy way to go about it is:
In general you can do a bunch of things:
BTW this method works regardless of whether you are creating a document-based app or not or whether it is a single-window or multi-window document based app.
The one downside to using view controllers is a lot of the top-level view layout and management has to be done programmatically and not in IB. But it's not that difficult.
Usually you just need to do a addSubview: and setFrame:, to place your views in a parent view you already laid out in IB.
But to do more complicated and manual stuff you should read up on the docs and get to know how the following work: frame/bounds, flipped coordinates, autoresizing, and how to override autoresizing to do your own layouts.
它可以按照您所描述的方式完成,但您必须手动管理与文档的关系,因为默认配置假定一个文档 -->为了简单起见,使用单控制器关系。
一旦脱离标准设计,您就会失去所有内置功能。
您可能想重新考虑您的设计。将文档链接到特定视图控制器的目的是为用户提供文档的隐喻。就用户而言,视图就是文档。如果您提供同一文档的多个视图,用户最有可能解释为具有多个文档,因为这是 UI 语法训练他们所期望的。
当您发现自己在 UI 中做了一些不标准的事情时,请停下来仔细思考。作为开发人员,您了解正在发生的事情并不意味着用户也会了解。
It can be done as you describe but you will have to manage the relationship to the document manually because the default configuration assumes a one-document --> one-controller relationship for simplicity sake.
Once move away from the standard design, you lose all the built-in functionality.
You might want to rethink your design. The purpose of linking a document to a specific view controller is to provide the user with a metaphor for the document. The view is the document as far as the user is concerned. If you provide multiple views of the same document, the user is most likely to interpret that has multiple documents because that is what the UI grammar has trained them to expect.
When you find yourselves doing something nonstandard in the UI, stop and think carefully. Just because you as the developer understands what is going on doesn't mean the user will.
使用 NSNavigationController 来管理多个视图怎么样?它可以轻松地推送和弹出各种视图?
What about using a NSNavigationController to manage the multiple views - it makes it easy to push and pop the various views ?