所以我有一个视图控制器,它有一个容器视图。容器视图嵌入了导航控制器,该控制器也是视图控制器的父控制器。故事板是这样的:
视图控制器(mainViewController
)--> 导航控制器--> 视图控制器(contentViewController
)
您可以在下面看到故事板的屏幕截图。
第一个箭头是从容器视图到导航控制器的嵌入序列。第二个箭头是一个关系,代表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
. 它在容器视图被点击时发送viewTapped:(id)sender
消息。mainViewController
在 的根视图内部contentViewController
,有一个按钮,点击时buttonTapped:(id)sender
会向其发送消息contentViewController
。工具栏中有一个条形按钮,点击时会向其contentViewController
发送barButtonTapped:(id)sender
消息contentViewController
。最初的场景是mainViewController
。当应用程序运行时,我发现只有栏按钮的触摸事件被阻止,触摸事件被按钮正确处理。在 Apple 的文档Regulating the Delivery of Touches to Views中,它说:
在简单的情况下,当触摸发生时,触摸对象从 UIApplication 对象传递到 UIWindow 对象。然后,在将触摸传递给视图对象本身之前,窗口首先将触摸发送到附加到发生触摸的视图(或该视图的超级视图)的任何手势识别器。
我认为触摸事件不会传递给按钮。这真的让我很困惑。有人可以解释这种行为吗?非常感谢。
故事板截图: