有没有更好的方法从子视图访问文档?

发布于 2024-12-13 20:20:20 字数 243 浏览 1 评论 0原文

我的文档上有一些类,例如 NSNotificationCenter 和 NSUndoManager,我需要从子视图访问它们。

现在我可以通过执行以下操作来访问它们:

NSUndoManager *undoManager = [[[[[self view] window] windowController] document] undoManager];

是否有我不知道的更好方法?谢谢。

I have some classes located on my Document such as NSNotificationCenter and NSUndoManager that I need access to from my subviews.

Right now I can access them by doing something like this:

NSUndoManager *undoManager = [[[[[self view] window] windowController] document] undoManager];

Is there a better approach I don't know about? Thanks.

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

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

发布评论

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

评论(1

赠意 2024-12-20 20:20:20

为什么视图需要担心撤消管理器?似乎需要进行一些重构。

通常,视图可能会触发第一响应者中的某些操作。然后,该操作会沿着响应者链向上传递,直到找到响应该操作的对象,例如窗口控制器或文档。 “撤消”事件通常会影响数据模型,因此文档会处理类似的事情。当用户撤消先前的操作时,模型将恢复到先前的状态,并且视图层次结构会反映更改。

在您的情况下,是否可以将处理撤消管理器的责任转移到文档中?

这同样适用于通知中心。如果文档有通知中心,为什么视图要尝试使用它?您可以利用如上所述的响应者链来向文档发送消息吗?如果更合适的话,您还可以使用 +defaultNotificationCenter 获取默认通知中心。

Why does a view need to worry about the undo manager? Seems like some refactoring might be in order there.

Normally, the view might trigger some action in the first responder. That action then gets passed up the responder chain until it finds an object, such as the window controller or document, that responds to that action. An 'undo' event generally affects the data model, so the document would handle something like that. When the user undoes a previous operation, the model is reverted back to a prior state, and the view hierarchy reflects the change.

Is it possible in your case to move the responsibility for dealing with the undo manager up to the document?

The same could apply to the notification center. If the document has a notification center, why is the view trying to use it? Can you leverage the responder chain as described above to get a message to the document? There's also a default notification center that you can get with +defaultNotificationCenter if that's more appropriate.

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