【发布时间】:2014-02-14 12:40:48
【问题描述】:
所以我有一个视图控制器,它有一个容器视图。容器视图嵌入了导航控制器,该控制器也是视图控制器的父控制器。故事板是这样的:
视图控制器(mainViewController) --> 导航控制器 --> 视图控制器(contentViewController)
您可以在下面看到故事板的屏幕截图。
第一个箭头是从容器视图到导航控制器的嵌入segue。第二个箭头是一个关系,代表contentViewController是导航控制器的根视图控制器。
mainViewController 和contentViewController 是同一类的对象,名为testViewController。它是 UIViewController 的子类。它的实现很简单。它只有三个IBAction 方法,没有别的。下面是实现代码:
#import "TestViewController.h"
@implementation TestViewController
- (IBAction)buttonTapped:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"button is tapped"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
- (IBAction)barButtonTapped:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"bar button is tapped"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
- (IBAction)viewTapped:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"view is tapped"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
@end
我在mainViewController 的容器视图中添加了一个点击手势识别器。当容器视图被点击时,它会向mainViewController 发送viewTapped:(id)sender 消息。在contentViewController 的根视图内部,有一个按钮,当点击该按钮时,它会向contentViewController 发送buttonTapped:(id)sender 消息。在contentViewController 的工具栏中有一个条形按钮,点击时会向contentViewController 发送barButtonTapped:(id)sender 消息。初始场景是mainViewController。当应用程序运行时,我发现只有栏按钮的触摸事件被阻止,触摸事件被按钮正确处理。在 Apple 的文档 Regulating the Delivery of Touches to Views 中,它说:
在简单的情况下,当发生触摸时,将触摸对象传递给 从 UIApplication 对象到 UIWindow 对象。然后,窗户 首先将触摸发送到附加在视图中的任何手势识别器 触摸发生(或该视图的超级视图),在它通过之前 对视图对象本身的触摸。
我认为触摸事件不会传递给按钮。这真的让我很困惑。有人可以解释这种行为吗?非常感谢。
故事板屏幕截图:
【问题讨论】:
标签: ios iphone event-handling uigesturerecognizer uicontainerview