UIView、UIViewController、UIApplication、UIWindow都是UIResponder子类,其中UIView是直接获得触摸事件,其他对象是通过响应对象链接收事件。下图是一个响应对象链。
可以看到UIResponder对象有一个nextResponder指针,当用户在UIView对象上操作后,该UIView对象会将其nextResponder指针指向父UIView对象,如果这个父UIView对象属于属于某个UIViewController对象时,其nextResponder指针就会指向包含该视图的UIViewController对象(如图中(1)所示)。当他不属于任何UIViewController对象时,其nextResponder指针就会指向该视图的父视图(如图中(2)所示)。最顶层的父视图是UIWindow对象,而UIWindow对象的nextResponder指向的是UIApplication实例(单实例)。UIWindow对象和UIApplication实例默认不会对事件做任何处理的,需要创建相应的子类,并重写相关事件处理方法才能处理特定的事件。
通常情况下,nextResponder是由UIResponder对象来发送消息的,也可以直接向nextResponder发送消息。如下代码,直接向nextResponder发送消息来处理连按事件。
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
if ([touche tapCount] == 2) {
[[self nextResponder] touchesBegan:touches withEvent:event];
return;
}
//处理其他事件
}
这样可以让父视图来处理连按事件