1

I have a view with several buttons that all have the same target and action.

At the moment the action is @selector(doSomething).

I need to be able to determine which button is pressed but I'm not sure of the best way to do this.

The method doSomething is declared as...

-(void)doSomething;

Is there a better way to declare this so I can get more info out?

Thanks for any help!

Oliver

Fogmeister
  • 73,141
  • 39
  • 203
  • 288

2 Answers2

8

If you declare the method as

- (void)doSomething:(UIButton *)sender

with the corresponding selector @selector(doSomething:), then you can check the identity of the sender using either of the methods Joshua suggested.

Noah Witherspoon
  • 56,657
  • 16
  • 129
  • 131
3

Target/action messages provide the sender as the argument. In your case, the button that called the message is the sender. You could ask it for its -tag (which you can set in IB) or you could name it as an outlet and test for equality: if (sender == myButtonOne) ...

Joshua Nozzi
  • 60,230
  • 14
  • 139
  • 134