views:

78

answers:

2

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?

+1  A: 

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.

Todd Yandell
A: 

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.

Peter Hosey