事件响应链

来源:互联网 发布:定做软件 猪八戒网 编辑:程序博客网 时间:2024/05/29 07:37

(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");    }


0 0