Cocoa 中单个窗口中的多个文档

发布于 2024-07-26 13:22:27 字数 327 浏览 8 评论 0原文

我想编写一个应用程序,它可以通过选项卡式界面在单个窗口中包含多个文档。 我应该避免使用 NSDocument 架构(基于 Cocoa Document 的应用程序模板)吗? 据我所知,它仅支持每个文档一个或多个窗口,但反之则不然。

我已经在这个问题上思考了一段时间,并且已经在 NSDocument 架构上构建了我的大部分应用程序,但我无法找到将多个文档与单个窗口关联的好方法。

编辑:除了基本文档窗口之外,我还想拥有项目文档窗口。 在这种复杂程度下,是否还值得破解 NSDocument 架构? Apple 是否使用 NSDocument 架构编写了 Xcode(以这种方式工作)?

I want to write an application which may have multiple documents in a single window via a tabbed interface. Should I avoid the NSDocument architecture (the Cocoa Document-based Application template)? As far as I can tell, it supports only one or more window per document but not vice versa.

I have been wrestling with this question for a while and have already built much of my application on the NSDocument architecture but I cannot figure out a good way to associate multiple documents with a single window.

EDIT: I want to have project document windows in addition to basic document windows. At this level of complexity, would it still be worth hacking the NSDocument architecture? Did Apple write Xcode (which works this way) using the NSDocument architecture?

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

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

发布评论

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

评论(4

不爱素颜 2024-08-02 13:23:55

另一种技术(我尚未尝试但计划尝试)是为每个文档设置一个无边框窗口。 这样,一个文档就有一个窗口,该窗口可能可见,也可能不可见。

然后,有一个包含实际窗口边框的包装器窗口,以及在可见的无边框文档窗口之间切换的任何控件。 文档窗口是包装器的子窗口,这确保当窗口移动/最小化/关闭等时,两者都将被链接。

对于每个无边框文档窗口,包装器窗口都有一个占位符视图,当调整大小时,将调整文档窗口的大小,并将文档窗口的视图注入到响应者链中(发送到占位符视图的任何事件,都将发送到文档窗口)在传递到占位符的父视图之前的窗口视图)。

虽然还有一些小细节需要解决,但我认为这种方法会很有效。

Another technique, which I haven't tried yet but plan to, is to have a borderless window for each document. This way one document has one window, which may or may not be visible.

Then, have a wrapper window containing the actual window border, and any controls to switch between which borderless document window/windows are visible. The document window is a child window of the wrapper, which ensures both will be linked when a window is moved/minimised/closed/etc.

For each borderless document window, the wrapper window has a placeholder view that, when resized, will resize the document window, and also inject the document window's view into the responder chain (any event sent to the placeholder view, will be sent to the document window's view before being passed on to the placeholder's parent view).

There are still some minor details to work out, but I think this approach will work well.

心病无药医 2024-08-02 13:23:44

几年前,我尝试将 NSDocument 应用程序硬塞到单窗口选项卡式界面中,但几个月后我感到非常沮丧,我回去重构了文档架构部分。 这并非不可能,但您最终要解决太多问题,以至于最终结果几乎不像一个正确的 NSDocument 应用程序。 最好只是重写您确实需要的部分,而不是仅仅为了破坏 Cocoa 框架而编写大量代码。

I tried shoehorning an NSDocument app into a single window tabbed interface a few years ago, and ended up so frustrated after a few months I went back and refactored out the document architecture pieces. It's not impossible, but you end up working around so many problems that the final result barely resembles a proper NSDocument app. It's better to just rewrite the bits you do need, than end up with a lot of code just to subvert the Cocoa frameworks.

ゝ杯具 2024-08-02 13:23:24

在这种情况下,使用基于 NSDocument 的架构不一定是一个坏主意; 但它可能需要相当多的 haquery。

很可能您不仅必须子类化 NSDocument,而且还必须子类化兄弟 NSDocumentController(很少见)。 完成此操作后,劫持并避免调用 -makeWindowControllers 和其他与窗口相关的方法应该是一件简单的事情,允许您以任何您喜欢的方式包装文档“窗口”,但仍然保留文档的好处 -基于应用程序。

Using an NSDocument-based architecture isn't necessarily a bad idea in this case; but it might require quite a bit of haquery.

It is quite likely that you will have to not only subclass NSDocument, but also it's more rarely subclassed sibling NSDocumentController. Once this is done, it should be a simple matter to hijack and avoid calls to -makeWindowControllers and other window-related methods, allowing you to wrap the document "windows" in whatever fashion you please, but still retain the benefits of the document-based application.

怪我太投入 2024-08-02 13:23:07

我有同样类型的项目——我想在一个窗口中呈现不同的独立文档,并带有一个允许在文档之间切换的侧边栏——所以我自己做了一些搜索。

我刚刚通过阅读 Cocoadev 的基于文档找到了一个有趣的线索使用一个窗口处理所有文档的应用程序参考。 从 MikeTrent 的回答来看,使用 NSDocument 似乎是一种非常可行的方法。 您只需要继承 NSDocumentController 即可。

我也喜欢 Abhi 的想法使用无边框子窗口。

I have the same kind of project — different independent documents that I want to present in a single window, with a sidebar that allows switching between documents — so I have done a little bit of searching myself.

I just found an interesting lead by reading Cocoadev's Document Based App With One Window For All Documents reference. It appears, from MikeTrent's answer, that using NSDocument is a very viable way to go. You just need subclassing NSDocumentController.

I also like Abhi's idea to use a borderless child window.

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