简体   繁体   中英

Tap gesture recognizer on image throws error when passing event

I have a UIImageView inside of a custom cell. I am creating a custom tap gesture recognizer for when the UIImageView is tapped to load another detail view.

The tap gesture is set up like so:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:event:)];
tapRecognizer.cancelsTouchesInView = YES;
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.delegate = (id)self;
[cell.userImage addGestureRecognizer:tapRecognizer];
cell.userImage.userInteractionEnabled = YES;

I am using imageTapped:event: so that I can detect what cell the user is tapping and load the data accordingly. The problem is I get this error:

在此输入图像描述

If I get rid of event like so, its works perfectly with no issues.

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];

I have used this method before in another application but for some reason I cannot get it to work properly with this error. Anyone recognize what this is? Thanks!

You need to read the documentation on UIGestureRecognizer ...

A gesture recognizer has one or more target-action pairs associated with it. If there are multiple target-action pairs, they are discrete, and not cumulative. Recognition of a gesture results in the dispatch of an action message to a target for each of those pairs. The action methods invoked must conform to one of the following signatures:

- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

Using one of these signatures will prevent the crash as you have already observed. You could then access the image view that was involved in the gesture by inspecting the recognizer's view property, and from this you will be able to access the appropriate UITableView cell.

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