在视图控制器中处理发送给第一响应者的消息

发布于 2024-08-06 12:02:43 字数 1880 浏览 14 评论 0原文

我一定错过了一些简单的东西,但我看不到它。首先,设置:

  • DATreeViewNSView的子类
  • 的子类
  • DATreeViewControllerNSViewController MainMenu 。 xib 有一个 DATreeViewController 实例,配置为从 DATreeView.xib 加载视图
  • MainMenu.xib 也有一个实例DendreaAppDelegate 的,它有一个 DATreeViewController 出口(连接到 MainMenu.xibDATreeViewController 实例。
  • Do Something!,一个菜单项,用于向第一响应者发送 doSomething: 消息
  • 并非巧合的是,DATreeViewController 有一个名为 <的操作。 ,

我想要的是 Do Something! 菜单项触发 DATreeViewController 上的 doSomething: 操作 假设我不能直接设置目标/操作连接,我会在这里停下来问,我的设计完全错误吗?我想做的事是愚蠢/邪恶/可能会让我在上帝眼中蒙羞吗?

不?伟大的。现在,在我的一生中,我无法使菜单项处于活动状态。我正在 Snow Leopard 上这样做,如果这有什么区别的话。

为了实现 NSViewNSViewController 之间的良好集成,例如管理响应者链,我遵循 Matt Gallagher 的示例,有一个实质性更改:在任何时候都没有NIB 加载过程中 NSView 似乎收到了 setViewController: 消息,因此我自己在 DATreeViewControllerloadView< 中发送该消息/代码> 消息。据我所知,在 applicationDidFinishLaunching: 中运行以下代码后,

NSView *view = self.treeViewController.view;
[self.window.contentView addSubview:view];

响应者链已按预期设置,即:

NSWindow < NSView < DATreeViewController < DATreeView

我的期望是,作为 DATreeViewController 是响应者链的一部分,并且由于它响应 doSomething:,并且由于它不实现任何验证,因此我所要做的就是使用 Interface Builder 来连接 Do Something!< /code> 菜单项发送给第一响应者代理,以 doSomething: 作为其操作,菜单项将自动处于活动状态。

我做错了什么?感谢大家的宝贵帮助!

I must be missing something simple, but I can't see it. First, the setup:

  • DATreeView, a subclass of NSView
  • DATreeViewController, a subclass of NSViewController
  • MainMenu.xib has an instance of DATreeViewController, which is configured to load a view from DATreeView.xib
  • MainMenu.xib also has an instance of DendreaAppDelegate, which has a DATreeViewController outlet (which is wired up to MainMenu.xib's DATreeViewController instance.
  • Do Something!, a menu item wired up to send a doSomething: message to the First Responder.
  • Not coincidentally, DATreeViewController has an action called doSomething:.

What I want is for the Do Something! menu item to trigger the doSomething: action on DATreeViewController, and let's pretend I can't just set the target/action connection directly. I'll stop right here and ask, is my design totally wrong? Is what I'm trying to do stupid/evil/likely to shame me in the eyes of my God?

No? Great. Now, for the life of me, I can't get the menu item to be active. I'm doing this on Snow Leopard, if that makes any difference.

In order to achieve good integration between NSView and NSViewController, e.g. managing the responder chain, I've followed Matt Gallagher's example, with one substantive change: at no point in the NIB loading process does NSView seem to receive a setViewController: message, so I send that message myself in DATreeViewController's loadView message. From what I can tell, after running the following code in applicationDidFinishLaunching:

NSView *view = self.treeViewController.view;
[self.window.contentView addSubview:view];

the responder chain is set up as expected, that is:

NSWindow < NSView < DATreeViewController < DATreeView

It was my expectation that, being as DATreeViewController is part of the responder chain, and being as it responds to doSomething:, and being as it implements no validation, all I would have to do is use Interface Builder to wire the Do Something! menu item to the First Responder proxy, with doSomething: as its action, and the menu item would be active automatically.

What am I doing wrong? Thank you all for your invaluable assistance!

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

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

发布评论

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

评论(3

那请放手 2024-08-13 12:02:43

您的观点是否接受并成功成为第一响应者?

Does your view accept and successfully become first responder?

埋情葬爱 2024-08-13 12:02:43

DATreeViewController 是否已连接到 IB 中 DATreeView 的 viewController 插座?

您是否通过 setViewController: 和 setNextResponder: 进行跟踪以验证 nextResponder 是否已正确设置?

Is DATreeViewController wired to the DATreeView's viewController outlet in IB?

Have you traced through setViewController: and setNextResponder: to verify that nextResponder is being set up properly?

水溶 2024-08-13 12:02:43

响应者链仅适用于 NSResponder 超类中的消息,例如 mouseDown mouseExited 等。我相信您可以通过向NSResponder 通过查看 nextResponder 并在 nextResponder 存在的情况下发送消息来冒泡其他方法。

这似乎是此技术的一个示例: https://github.com /MrNoodle/NoodleKit/blob/master/NSResponder-NoodleModalExtensions.m

The responder chain only works for messages in the NSResponder superclass such as mouseDown mouseExited etc. I believe you can do something sneaky by adding a category to NSResponder to bubble up other methods by looking at the nextResponder and sending the message up if nextResponder exists.

This appears to be an example of this technique: https://github.com/MrNoodle/NoodleKit/blob/master/NSResponder-NoodleModalExtensions.m

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