With arrays.
for (UIButton *button in self.arrayOfButtons) {
[button addTarget:self
action:@selector(buttonDown:)
forControlEvents:UIControlEventTouchDown];
[button addTarget:self
action:@selector(buttonUp:) // it's cold outside
forControlEvents:UIControlEventTouchUpInside];
}
Your buttonDown:
and buttonUp:
methods should look like this:
- (void)buttonDown:(id)sender
OR
- (void)buttonDown:(UIButton *)button
Either way, sender
or button
is going to be a reference to the object that has called the method.
If you have btnFoo
and btnBar
and you give both of them buttonDown:
for the touch down event, then sender
or button
will tell you WHICH button was pressed to get into the method.
To more completely see what's going on, give all your buttons a tag, and throw this line of code in the method:
NSLog(@"Button.tag = %d", button.tag);