为什么只有某些操作才必须调用 setTarget?
对于大多数操作,我只需在 InterfaceBuilder 中单击并拖动即可将某个接口对象的调用“连接”到我的代码。例如,如果我想知道用户何时单击表中的一行,我将连接从表的操作拖动到控制器的操作。
但现在让我们考虑用户双击一行。如果我希望在发生这种情况时调用我的操作之一,我不仅需要调用 -[NSTableView setDoubleAction]
,而且还 -[NSControl setTarget]< /代码>。为什么?
需要明确的是,我不是问为什么 Interface Builder 不支持 setDoubleAction
。所有工具都有局限性。我试图更好地了解如何以及为什么 setTarget
似乎没有必要,除非并且直到我希望 setDoubleAction
工作。提出这个问题的另一种方法是:为什么我不需要在 Interface Builder 中执行任何操作来设置表(单击)操作的目标?
For most actions, I just click and drag in InterfaceBuilder to "wire up" a call from some interface object to my code. For example, if I want to know when the user single-clicks a row in a table, I drag a connection from the table's action to my controller's action.
But now let's consider the user double-clicking a row. If I want one of my actions to be called when this happens, I need to call not only -[NSTableView setDoubleAction]
but also -[NSControl setTarget]
. Why?
To be clear, I am not asking why Interface Builder doesn't support setDoubleAction
. All tools have limitations. I am trying to gain a greater understanding about how and why setTarget
doesn't seem to be necessary unless and until I want setDoubleAction
to work. Another way to ask this question would be: Why don't I need to do anything in Interface Builder to set the target of the table's (single-click) action?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将表视图连接到 IB 中的某个操作,然后对其调用
setDoubleAction
,则无需额外调用setTarget
。但是,如果您只想接收双击消息,并且没有将表视图连接到 IB 中的操作,则您必须调用setTarget
。表视图会将
action
和doubleAction
发送到同一目标。你可以想象 NSTableView 是这样实现的:而你在 IB 中所做的事情是这样的:
真正的实现与此相去甚远,但这实际上就是正在发生的事情。
If you connect your table view to an action in IB, then call
setDoubleAction
on it, there should be no need to make an additional call tosetTarget
. However, if you only wish to receive the double click message, and you didn’t connect the table view to an action in IB, you will have to callsetTarget
.A table view will send
action
anddoubleAction
to the same target. You can imagine NSTableView as being implemented like this:And what you’re doing in IB is something like this:
The real implementations are nowhere close to that, but that is effectively what’s going on.
如果您设置操作(或双击操作)但未设置目标(或将目标设置为
nil
),则操作消息将通过响应者链。如果除了操作之外还设置了目标,则操作消息将仅发送至该对象。
If you set an action (or double-click action) and don't set a target (or set the target to
nil
), then the action message will go through the responder chain.If you set a target in addition to an action, the action message will go only to that object.