views:

184

answers:

3

Within an action method such as

- (IBAction)myAction:(id)sender {
// do something
}

what can the sender parameter be used for?

Is it possible to detect what type of click (such as left mouse down) was made on the control that invoked the action? If so how?

+5  A: 

It depends a lot on the situation. The sender is the object that sent the action message, so you can do anything with that object that you could do in any other context. There's nothing special about the parameter.

For one example, you might do [someTextField takeIntegerValueFrom:sender] to have a text field that shows the value of a slider bar.

If the sender offers some way to tell which button was pressed, then you could do that. I don't know of any class that does that, though. It would be a kind of awkward design. If different clicks are supposed to do different things, it would be better for them to have different action methods.

Chuck
+2  A: 
switch([sender tag])
{
 case FOO_BUTTON_TAG: 
     // do foo
  break;

 case BAR_BUTTON_TAG:
     // do bar
  break;

   &c
}
kent
+1  A: 

It is useful when few controls (e.g. button cells in a table) send the same action message. Sender can be identified by an id or by comparation (sender == oneOfMyIBOutlets)

vaddieg