Search code examples
objective-cios4xcode4.5

[UITapGestureRecognizer tag]: unrecognized selector sent to instance


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


Solution

  • 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