nibeck
nibeck

Reputation: 669

Gesturerecognizer on sub-view, getting error

I have a subview (UIImageView) added to my main view. I want to detect taps in the subview only, and then run the 'processTap' method.

I get the following exception when it detects a click.

"NSInvalidArgumentException', reason: '-[UIImageView processTap]: unrecognized selector sent to instance"

Something seems to be wrong with the @selector portion.

Any ideas?

- (void)viewDidLoad
{
  // Create a uiimage view, load image
  UIImageView *tapView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tap zone.png"]];
  tapView.frame = CGRectMake(20, 50, 279, 298);
  tapView.userInteractionEnabled = YES;

  [self.view addSubview:tapView];

  // Initialize tap gesture recognizers
  UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                     initWithTarget:tapView 
                                     action:@selector(processTap)];
  singleTap.numberOfTapsRequired = 1;

  [tapView addGestureRecognizer:singleTap];

  [super viewDidLoad];
}

- (void)processTap {
    if (sessionRunning) {
        tapCounter = tapCounter + 1;
        _tapCount.text = [NSString stringWithFormat:@"%i",tapCounter];
    }
}'

Upvotes: 1

Views: 1060

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

The target for your recognizer has to be self.

// Initialize tap gesture recognizers
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                     initWithTarget:self 
                                     action:@selector(processTap)];

You have the previous error because your tapView hasn't a selector called processTap.

NOTE

From Apple's documentation:

target

An object that is the recipient of action messages sent by the receiver when it recognizes a gesture. nil is not a valid value.

So, in this case the recipient for your action is the controller that implements processTap selector.

Upvotes: 3

Related Questions