简体   繁体   中英

Adding a gesture in code gives NSInvalidArgumentException

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.

PS 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;

}
[self.toyImageTappable addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToyImageTap:)]];

目标应该是您观察要调用的操作方法的对象,在这种情况下为self

[self.toyImageTappable addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToyImageTap:)]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM