简体   繁体   中英

TapGestureRecognizer not calling selector

So I have a custom view that I am creating many of in a for-loop in my viewcontroller. While they're being created, I'm calling this method on each of them:

-(void)setUpStuff{
    //Random setup code
    [self setUserInteractionEnabled:YES];
    tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self   action:@selector(tileSelected:)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:tapGesture];
}

And this is the selector it should be calling:

-(void)tileSelected{
    NSLog(@"Why am I not working?");
    [self.delegate tileSelected:self];
}

It's probably worth mentioning that this customView has an imageView and label embedded within it. I've tried adding the gesture recognizer to the imageView and making sure that the imageView is being brought to the very front, however, that doesn't work either.. Any ideas?

Your selector call is wrong

it should be:

tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self   action:@selector(tileSelected)];

without the :

首先需要设置视图的用户交互功能,然后再添加手势,选择器还包括:方法末尾,这意味着您需要在方法中添加参数,或者只是从选择器末尾删除:。

you can change your tileSelected func to :`

-(void)tileSelected:(UITapGestureRecognizer*)gesture{
    NSLog(@"Why am I not working?");
   [self.delegate tileSelected:self];
}

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