0

I created a UIView subview which I add to my UIViewController in storyboard, I want to respond to a tap on the subview and call the superview's implementation of the following method. The call works properly, but it doesn't pass as an argument the proper touch.tapcount to the superview's implementation.

Here is the method in the subview:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    CGRect rect = [[UIScreen mainScreen] bounds];
    UITouch *touch = [[event touchesForView:self]anyObject];
    CGPoint location = [touch locationInView:self];

    if(touch.tapCount == 2){

        [self.superview touchesBegan:touches withEvent:event];

    }
}

Here is the method in the superview, the if block is not executed:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    CGRect rect = [[UIScreen mainScreen] bounds];
    UITouch *touch = [[event touchesForView:self]anyObject];
    CGPoint location = [touch locationInView:self];

    if(touch.tapCount == 2){

      //Code is not executed here
    }
}

Solution

change this statement in superview:

UITouch *touch = [[event touchesForView:self]anyObject];

to this:

UITouch *touch = [[event touchesForView:[self.view.subviews objectAtIndex:1]] anyObject];
Michael
  • 6,561
  • 5
  • 38
  • 55
  • 1
    In the superview, have you verified that `touch` is not `nil`? – rmaddy May 07 '14 at 17:51
  • Yes, touch is nil in the superview. – Michael May 07 '14 at 17:53
  • `anyObject` may return *any object*. There's a clue in the name. So there's no reason to suppose the two `touch`es are the same touch, even if `touchesForView:` were returning the same set, which it seemingly isn't. – Tommy May 07 '14 at 17:53
  • 2
    @Nikita It's `nil` because the touch isn't for the superview so calling `[event touchesForView:self]` in the superview gives you no results. – rmaddy May 07 '14 at 17:54

0 Answers0