为什么只有某些操作才必须调用 setTarget?

发布于 2024-08-26 22:59:51 字数 528 浏览 4 评论 0原文

对于大多数操作,我只需在 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 技术交流群。

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

发布评论

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

评论(2

寂寞美少年 2024-09-02 22:59:51

如果您将表视图连接到 IB 中的某个操作,然后对其调用 setDoubleAction,则无需额外调用 setTarget。但是,如果您只想接收双击消息,并且没有将表视图连接到 IB 中的操作,则您必须调用setTarget

表视图会将 actiondoubleAction 发送到同一目标。你可以想象 NSTableView 是这样实现的:

@implementation NSTableView

- (void)theUserClickedOnMe
{
   [self sendAction:[self action] to:[self target];
}

- (void)theUserDoubleClickedOnMe
{
   [self sendAction:[self doubleAction] to:[self target]];
}

@end

而你在 IB 中所做的事情是这样的:

- (void)userConnectedControl:(NSControl *)control
        toAction:(SEL)action
        ofObject:(id)object
{
   [control setTarget:object];
   [control setAction:action];
}

真正的实现与此相去甚远,但这实际上就是正在发生的事情。

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 to setTarget. 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 call setTarget.

A table view will send action and doubleAction to the same target. You can imagine NSTableView as being implemented like this:

@implementation NSTableView

- (void)theUserClickedOnMe
{
   [self sendAction:[self action] to:[self target];
}

- (void)theUserDoubleClickedOnMe
{
   [self sendAction:[self doubleAction] to:[self target]];
}

@end

And what you’re doing in IB is something like this:

- (void)userConnectedControl:(NSControl *)control
        toAction:(SEL)action
        ofObject:(id)object
{
   [control setTarget:object];
   [control setAction:action];
}

The real implementations are nowhere close to that, but that is effectively what’s going on.

っ左 2024-09-02 22:59:51

如果您设置操作(或双击操作)但未设置目标(或将目标设置为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.

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