事件响应链

2015-01-27 14:01:16 · 作者: · 浏览: 28

(1)设置根视图控制器

(2)RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    MyView *view = [[MyView alloc] initWithFrame:CGRectMake(0, 20, 320, 300)];
    view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"viewCtrl touchesBegan");
    
    [self.nextResponder touchesBegan:touches withEvent:event];
}

(3)MyView.m

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"myView touchesBegan");
    
    //将事件传递给下一个响应者
    /*
     nextResponder可以取得下一个响应者
     */
    [self.nextResponder touchesBegan:touches withEvent:event];
}

(4)TouchWindow.m

//window分发事件的方法
- (void)sendEvent:(UIEvent *)event {

    NSLog(@"window sendEvent");
    
    [super sendEvent:event];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"window touchesBegan");
    
}