子视图阻止父视图TouchesBegan?

时间:2012-10-10 04:17:04

标签: iphone ios cocoa-touch uitableview

我有一个UITableView作为subview的视图,它完全覆盖了父视图。当我尝试为父视图实现touchesBegan时,我注意到该方法根本没有被调用。无论如何都要覆盖这种行为?

4 个答案:

答案 0 :(得分:12)

实际上有一种方法可以覆盖这种行为。您需要扩展阻止父视图的每个子对象,并覆盖touchesBegan方法,如下所示:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   [super touchesBegan:touches withEvent:event];
   [self.nextResponder touchesBegan:touches withEvent:event];
}

例如,如果在UIVcrollView中有一个UIButton,它位于UIView内,那么你需要创建两个自定义类。一个扩展UIButton和其他扩展UIScrollView。在每个自定义类中,您需要添加上述方法定义。完成这些更改后,您将能够在父UIView中收到touchesBegan事件。

答案 1 :(得分:3)

touchesBegan方法与父视图的框架有关。如果父视图看不到框架并且subviews完全包围该框架,则父视图将不会接收到触摸。

答案 2 :(得分:1)

您也可以设置:

[theViewThatIsCoveringParent setUserInteractionEnabled:NO];

这样所有接触都会被父母抓住。

答案 3 :(得分:0)

执行此操作:Why isn't my UIViewController in the responder chain for its view?

简而言之:

  • 将您的观点链接到您的控制器。
  • 在视图中添加一个名为nextResponderPlusSomething的属性(已有nextResponder),此内容正在接触
  • viewdidload中将此属性设置为控制器(自我)
  • touchesBegan内添加对nextResponderPlusSomething:touchesBegan
  • 的调用
  • 在控制器上实现此事件,将事件发送到您要调用的视图。