[UITapGestureRecognizer tag]: unrecognized selector sent to instance
Asked Answered
D

5

13

I am having a series of imageview arranged, and assigning a TapView recognizer to it

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                                         initWithTarget:self action:@selector(action:)];
[tapRecognizer setNumberOfTouchesRequired:1];
[tapRecognizer setDelegate:self];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:tapRecognizer];

and I have defined the selector as:

-(void) action:(id)sender
  {
    NSLog(@"TESTING TAP");
    NSLog (@"%d",[sender tag]);
  }

This is getting Crashed and i am getting Error message as:-

[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x145d0210

Disfrock answered 3/10, 2013 at 6:48 Comment(0)
L
38

You can use this..

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                                         initWithTarget:self action:@selector(action:)];
[tapRecognizer setNumberOfTouchesRequired:1];
[tapRecognizer setDelegate:self];
imageView.userInteractionEnabled = YES;
imageView.tag = 1111;
[imageView addGestureRecognizer:tapRecognizer];

And in action try this..

-(void) action:(id)sender
  {
    NSLog(@"TESTING TAP");
    UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer *)sender;
    NSLog (@"%d",[tapRecognizer.view tag]);
  }

Explaination:

UITapGestureRecognizer has not property like tag. but it has property view, from this property you can access the view with which UITapGestureRecognizer was attached.

Hope it will help you

Luetic answered 3/10, 2013 at 7:9 Comment(2)
Welcome and feel free for any help. And please accept the answer so that other users can get help from your postLuetic
swift equivalent please?? @NiravGadhiyaSleave
C
10

Just Change your Selector Method with the following..and it will work

tapgesture will have the whole view which is tapped.. and then you can get the tag property from it as i have stated in following

-(void)action:(UITapGestureRecognizer *)tapGesture{

     NSLog(@"TESTING TAP");
        NSLog (@"%d",tapGesture.view.tag);

    }
Chamfron answered 3/10, 2013 at 7:31 Comment(0)
V
7

Neither UITapGestureRecognizer nor UIGestureRecognizer declares a property or method called tag.

You can't use it. That's why you're getting the error.

On a related note. I really don't like using tag in general. There is always a better way to do what you're doing without using tag.

Viviyan answered 3/10, 2013 at 6:52 Comment(4)
@Disfrock What are you trying to do with tag? i.e. why are you using it? If I know that I can prob suggest a better way.Viviyan
Actually, i have a series of imageview arranged and when it scroll through the imageview i shud get the preview..as we have in iphone gallery(ios7)Disfrock
Ah, the iPhone Photos app uses a UICollectionView to lay out the images. It works a lot like a UITableView and does all the tapping for you. That way you don't need the UITapGestureRecognizer at all and it's a lot more memory efficient to display all the photos at the same time.Viviyan
I'd suggest looking at this tutorial... raywenderlich.com/22324/…Viviyan
C
5

Swift equivalent of the accepted answer:

//use this to set up your tap gesture
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.reactToTap(sender:)))
imageView.tag = 33
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGesture)

This is the function which gets triggered by the tap:

@objc private func reactToTap(sender: UITapGestureRecognizer) {
            
    print("ImageView tag: ", sender.view?.tag ?? 0) //"ImageView tag: 33"
}
Concupiscence answered 11/8, 2020 at 22:21 Comment(2)
this works! I was looking for a Swift solution for ages thanks!Syncrisis
For anyone who is looking for a solution, do not use sender as a view. use this solution and it will work. Thanks!Discalced
F
3

You cannot get tag property of UITapGestureRecognizer rather you have to get of its view's property,

You can try,

-(void)action:(id)sender
  {
     NSLog(@"TESTING TAP");
     NSLog (@"%d",[[sender view]tag]);

  }
Footy answered 3/10, 2013 at 7:7 Comment(1)
Thanks a lot for this. The other methods did not work for me!Lamrert

© 2022 - 2024 — McMap. All rights reserved.