从共享实用程序窗口和最前面的文档窗口进行绑定的好方法是什么?
我有一个允许打开多个 NSDocument 的应用程序。 在此应用程序中有一个实用程序窗口,其中包含我想要应用于最前面文档的一些功能。
我尝试在这里使用绑定,所以技巧是如何将实用程序窗口的用户界面干净地绑定到最前面的文档。 目标是切换最前面的文档窗口将更新实用程序窗口中的视图; 当文档模型的状态发生变化等时,绑定到最前面文档模型的属性的控件将被适当更新。
为了从这样的窗口发送操作,很容易只使用第一响应者; 文档对象可以通过响应者链拦截操作。 但我想要的不仅仅是这个,当然你不能绑定到第一响应者。
我有一些想法:
- 在我的笔尖中为共享窗口放置一个对象控制器。 当文档窗口更改最前面的状态时,更改该绑定的内容。 这样做的缺点是,如果我有另一种实用程序窗口,我还必须记住将文档窗口的绑定连接到该实用程序窗口!
- 在应用程序委托中创建一个访问器,通过遍历窗口列表来获取最前面的文档窗口。 我的实用程序窗口将仅通过应用程序委托的方法进行绑定。 这里的缺点是它不符合 KVO 标准
- 在应用程序委托中有一个 getter 和 setter 来确定(也许设置为 KVO 兼容?这有意义吗?)最前面的文档。 当窗口失去主状态时,也许可以使用窗口通知将 ivar 设置为适当的值。 更新:我现在正在使用这个,它实际上看起来很干净。 我从文档窗口的 windowDidBecomeMain 通知中设置值,并在 windowWillClose 中清除它(如果它是当前值)。 除非有任何重大反对意见,否则这可能是我将使用的方法。
- 一个想法是绑定到 mainWindow.windowController.document ...这很接近,只是当我的共享窗口成为主窗口时,这个绑定就会消失。 所以我真的需要找到最前面的文档窗口的控制器(以及正确的类)。
这些似乎都不完全正确。 有没有更好的方法来做到这一点,我错过了?
I have an application which allows for multiple NSDocuments to be open. In this application is a single utility window that contains some functionality that I want to apply to the frontmost document.
I am trying to use bindings here, so the trick is how to cleanly bind the user interface of the utility window to the frontmost document. The goal is that then switching the frontmost document window will update the view in the utility window; controls that are bound to properties of the frontmost document's model would be updated appropriately when state changes in the document's model, etc.
For sending actions from such a window, it's easy to just use first responder; the document object can intercept actions via the responder chain. But I want more than this, and of course you can't bind to the first responder.
A few ideas that I have:
- put an object controller in my nib for the shared window. When a document window changes frontmost status, change the content of that binding. A disadvantage of this is that if I were to have another kind of utility window, I'd have to remember to hook up the bindings from the document window to that utility window too!
- Make an accessor in the application delegate that gets the frontmost document window by traversing the window list. My utility window would just bind through the application delegate's method. A disadvantage here is that it's not KVO compliant
- Have a getter and setter in the application delegate to determine (and perhaps set to be KVO-compliant? would that make sense?) the frontmost document. Perhaps use window notifications set an ivar to the appropriate value when a window loses main status. Update: I'm using this for now, and it actually seems pretty clean. I set the value from the windowDidBecomeMain notification of my doc window and clear it (if it's the current value) in windowWillClose. Unless there is any major objection, this is probably the approach I'll use.
- One idea was to bind to mainWindow.windowController.document ... this comes close, except that when my shared window becomes main, then this binding goes away. So really I need to find the frontmost document window's controller (and of the right class).
None of these seem quite right. Is there a better way to do this that I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我总是通过共享应用程序
mainWindow.document
进行绑定,效果很好。 如果您的 Windows 没有文档,您可能需要添加一个mainYourKindOfWindow
键,该键是通过监视mainWindow
并根据某些过滤条件更新值来实现的。I’ve always bound through Shared Application,
mainWindow.document
, which works fine. if you have windows w/o documents, you may want to add amainYourKindOfWindow
key that is implemented by watchingmainWindow
and updating the value based on some filter criteria.Leopard 的 TextEdit 为其检查器执行此操作。 在 file:///Developer/Examples/AppKit/TextEdit 中查看。
Leopard's TextEdit does this for its inspector. Check it out in file:///Developer/Examples/AppKit/TextEdit.
这对我来说最有意义。 您可以将内容更改为文档实例 (
[NSDocumentController currentDocument]
)。啊? 我不明白这一点。
That makes the most sense to me. You'd change the content to the document instance (
[NSDocumentController currentDocument]
).Huh? I don't understand this.
在 TextEdit 中,检查器值通过中间对象控制器绑定。 控制器内容对象绑定到共享应用程序 mainWindow。
您可以将内容绑定到 mainWindow.firstResponder 并取消选中“针对不适用的键引发”。
In TextEdit, inspector values are bound via an intermediate object controller. The controller content object is bound to the shared application mainWindow.
You may bind the content to mainWindow.firstResponder and uncheck "Raises for not applicable keys".
使用键窗口,而不是主窗口。 NSApplication 的 keyWindow 属性可能不支持 KVO,但如果它不起作用,您仍然可以使用 NSNotifications。 这样做的原因是 NSDocumentController 的 currentDocument 使用 keyWindow,因此它更好地代表了内置功能。 此外,还可以设置面板以避免成为关键窗口。
Use the key window, not the main window. KVO might not be supported for NSApplication's keyWindow property, but you can still use NSNotifications if it doesn't work. The reason for this is that NSDocumentController's currentDocument uses the keyWindow, so it better represents the built in functionality. Also, panels can be set to avoid becoming key window.