Reputation:
I have a UIView on the view controller's view. A pan gesture is added on the UIView. Now I want to transfer the touch to the parent view (view controller's view) so that the touches delegate methods also gets called of parent view as well as the UIView is also panned.
Upvotes: 4
Views: 2263
Reputation: 3664
Depends on what you want to do. If you want to let the view controller know that something happened in the UIView child, you should pass a delegate of the main view controller to the child view (the Object-Oriented-Programming way). Something like this:
// in child UIVIew
...
id<mainControllerDelegate> _mainControllerDel; // This delegate was passed to the view by the main view controller
...
-(void)gestureHappened
{
[_mainControllerDel gestureHappenedInView];
}
But if you want both views to react to the gesture you should use the shouldRecognizeSimultaneouslyWithGestureRecognizer gesture delegate method, like this:
// In class that conforms to your UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
EDIT:
I've just read you want the touchesBegan
(and similar) method called in the parent also. That is not standard behaviour for any UI. Please read up on iOS event responder chain. If you really want the next view in chain to be called you can override the child's methods and call the next responder. Like this:
// in child view
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
What I would do is something similar but using a delegate:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_mainControllerDel touchBeganOnView: self withEvent: event];
}
Upvotes: 4