取决于你想做什么。如果你想让视图控制器知道 UIView 子视图中发生了某些事情,你应该将主视图控制器的委托传递给子视图(面向对象编程方式)。像这样的事情:
// in child UIVIew
...
id<mainControllerDelegate> _mainControllerDel; // This delegate was passed to the view by the main view controller
...
-(void)gestureHappened
{
[_mainControllerDel gestureHappenedInView];
}
但是如果您希望两个视图都对手势做出反应,您应该使用应与手势识别器同时识别 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer手势委托方法,如下所示:
// In class that conforms to your UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
EDIT:
我刚刚读到你想要touchesBegan
(和类似的)方法也在父级中调用。这不是任何 UI 的标准行为。请阅读iOS 事件响应链 https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html。如果您确实希望调用链中的下一个视图,您可以覆盖子级的方法并调用下一个响应者。像这样:
// in child view
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
我会做类似的事情,但使用委托:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_mainControllerDel touchBeganOnView: self withEvent: event];
}