0

While developing a demo app, I added a few gestures in XCode and they worked fine. However, adding gestures in code is giving an NSInvalidArgumentException at runtime, when gesture should be invoked. I was trying to add this gesture to an ImageView, but later I also attempted it with self.view. All to no avail :(

It's probably some memory related issue, but I'm not able to resolve it. Any help would be highly appreciated.

P.S. User interaction is enabled for imageView - the code worked fine when gestures were added in XCode.


This is what the debugger shows:

'NSInvalidArgumentException', reason: '-[UIView handleToyImageTap:]: unrecognized selector sent to instance.

Here is the code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    previousRotation = 4.0;
    [self.toyImageTappable addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self.toyImageTappable action:@selector(handleToyImageTap:)]];

}

- (void) handleToyImageTap:(UITapGestureRecognizer *)sender {

    //Rotate image by 45 degree
    self.toyImageTappable.transform = CGAffineTransformMakeRotation(M_PI/previousRotation);

    previousRotation = previousRotation + 4.0;
    if (previousRotation == 16.0)
        previousRotation = 4.0;

}
UVAL
  • 11
  • 3

2 Answers2

0
[self.toyImageTappable addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToyImageTap:)]];
Astri
  • 575
  • 4
  • 10
0

The target should be the object you watch the action method to be called on, in this case self:

[self.toyImageTappable addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToyImageTap:)]];
grahamparks
  • 16,130
  • 5
  • 49
  • 43
  • I tried that too :(, although, in this case, I want my target to be imageView itself. – UVAL Nov 16 '13 at 18:52
  • Okay, I tried this: target self, as you said, and added the recognizer to UIImageView. It did the job for me :) Thanks for your help. – UVAL Nov 16 '13 at 19:04