3

I've got a button called myButton and I gave it a UIGestureRecognizer so that an IBAction is only run when myButton is pressed with two fingers:

UIGestureRecognizer  *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerTap:)];
[(UITapGestureRecognizer *) tapper setNumberOfTouchesRequired:1];
[newTaskButton addGestureRecognizer:tapper];

Prior to adding the gesture recognizer, I could use sender to reference the button that was just pressed, however now sender is now the gesture recognizer. I still need to reference the button that was pressed...Is there any way to go about doing so? An easy method that returns whatever is using the gesture recognizer maybe? Thanks!

hemlocker
  • 1,012
  • 1
  • 9
  • 24
  • 2
    Unrelated to the question: If you change the declaration to `UITapGestureRecognizer *tapper`, you won't need to cast it on the next line. –  Jul 26 '11 at 19:20

1 Answers1

16

The UIGestureRecognizer class has a view property representing the view the gesture recognizer is attached to, in your case, the button.

- (void)twoFingerTap:(UIGestureRecognizer *)sender {
    UIButton *myButton = (UIButton *)sender.view;
}
albertamg
  • 28,492
  • 6
  • 64
  • 71
  • now, I don't believe it is affecting the performance of the application, however when I tell it to set the `title` property of `[sender view]`, I'm getting a warning that `UIView` may not respond to `setTitle`. I know thats because the compiler doesn't know that `[sender view]` is a `UIButton`. Is there any way to reassure the compiler so that the warnings go away? – hemlocker Jul 26 '11 at 19:44
  • @ghostware cast it! (see the snippet of code I added to my answer) – albertamg Jul 26 '11 at 19:51