在视图控制器中处理发送给第一响应者的消息
我一定错过了一些简单的东西,但我看不到它。首先,设置:
DATreeView
,NSView
的子类- 的子类
DATreeViewController
,NSViewController
MainMenu 。 xib 有一个DATreeViewController
实例,配置为从DATreeView.xib
加载视图MainMenu.xib
也有一个实例DendreaAppDelegate
的,它有一个DATreeViewController
出口(连接到MainMenu.xib
的DATreeViewController
实例。Do Something!
,一个菜单项,用于向第一响应者发送doSomething:
消息- 并非巧合的是,
DATreeViewController
有一个名为 <的操作。 ,
我想要的是 Do Something!
菜单项触发 DATreeViewController
上的 doSomething:
操作 假设我不能直接设置目标/操作连接,我会在这里停下来问,我的设计完全错误吗?我想做的事是愚蠢/邪恶/可能会让我在上帝眼中蒙羞吗?
不?伟大的。现在,在我的一生中,我无法使菜单项处于活动状态。我正在 Snow Leopard 上这样做,如果这有什么区别的话。
为了实现 NSView
和 NSViewController
之间的良好集成,例如管理响应者链,我遵循 Matt Gallagher 的示例,有一个实质性更改:在任何时候都没有NIB 加载过程中 NSView
似乎收到了 setViewController:
消息,因此我自己在 DATreeViewController
的 loadView< 中发送该消息/代码> 消息。据我所知,在
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 ofNSView
DATreeViewController
, a subclass ofNSViewController
MainMenu.xib
has an instance ofDATreeViewController
, which is configured to load a view fromDATreeView.xib
MainMenu.xib
also has an instance ofDendreaAppDelegate
, which has aDATreeViewController
outlet (which is wired up toMainMenu.xib
'sDATreeViewController
instance.Do Something!
, a menu item wired up to send adoSomething:
message to the First Responder.- Not coincidentally,
DATreeViewController
has an action calleddoSomething:
.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的观点是否接受并成功成为第一响应者?
Does your view accept and successfully become first responder?
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?
响应者链仅适用于
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 asmouseDown
mouseExited
etc. I believe you can do something sneaky by adding a category toNSResponder
to bubble up other methods by looking at thenextResponder
and sending the message up ifnextResponder
exists.This appears to be an example of this technique: https://github.com/MrNoodle/NoodleKit/blob/master/NSResponder-NoodleModalExtensions.m