具有共享窗口的基于可可文档的应用程序?
我正在开发一个基于文档的应用程序。每个文档都有三个窗口(因此有三个窗口控制器)。我想对其进行设置,以便在不同的打开文档之间共享三个窗口中的两个(根据需要交换视图)。这可能吗?谁能指出我正确的方向(文档或示例)?
谢谢!
I'm developing a document based app. Each document has three windows (and hence three window controllers). I'd like to set it up so that two of the three windows are shared between different open documents (swapping views as needed). Is this possible? Can anyone point me in the right direction (documentation or examples)?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,这些共享窗口控制器不应该由任何文档拥有(因为每个文档都有自己的一对“共享”窗口),而应该是独立的,可能由应用程序委托或文档控制器拥有。您可能还想制作窗户面板,就像检查员一样。
您需要让每个控制器跟踪哪个窗口是主窗口,并在主窗口更改时相应地更新其窗口,因为新的主窗口可能有不同的文档。
几乎任何有关如何制作检查器窗口的教程都会对您有所帮助。
In that case, these shared window controllers should not be owned by any document (since each document would then have its own pair of the “shared” windows), but should be independent, probably owned by the app delegate or the document controller. You may also want to make the windows panels, as an Inspector would be.
You'll want to have each controller track which window is main, and update its window accordingly when the main window changes, because the new main window may have a different document.
Pretty much any tutorial on how to make an Inspector window will help you here.
看起来您需要重写
NSDocument
子类中的-makeWindowControllers
来创建所需的控制器,在 NSDocument 子类上调用-addWindowController:
添加您的共享窗口控制器。我还没有这样做,但这些是我会考虑的方法。
来自 Apple 的 NSDocument 类参考:
It looks like you need to override
-makeWindowControllers
in yourNSDocument
subclass to create the controllers you want, invoking-addWindowController:
on the NSDocument subclass to add your shared window controllers.I haven't yet had to do this, but those are the methods I'd be looking at.
From Apple's NSDocument class reference:
这是可能的,但这需要您付出大量的工作。总之,您需要执行以下操作:
setDocument:
并维护它与每个文档的关联。NSWindowController
) 在窗口关闭之前将其自身与文档解除关联。对于可能处理窗口内视图的每个视图控制器也是如此。NSDocumentController
) 并负责文档关闭,以确保在关闭任何文档之前将多文档窗口与文档分离。NSDocumentController
是一个单例,因此您需要在MainMenu.xib
文件中添加一个实例来替换默认实例。您可以在此处阅读我的如何添加对多文档窗口控制器的支持的分步指南< /a>.
It is possible, but it'll take a non-trivial amount of work from your side. In summary here's what you need to do:
setDocument:
in the window controller and maintain the associations that it has to each document.NSWindowController
) disassociates itself from the document before the window is closed. The same goes for each view controllers that may be handling views inside the window.NSDocumentController
) and take care of document closing to make sure that multi-document windows are detached from documents before any of the documents are closed.NSDocumentController
is a singleton and thus you need to add an instance in yourMainMenu.xib
file to replace the default one.You can read my step-by-step guide how to add support for multi-document window controllers here.