今天发现一个偶现的BUG,当点击一个UIButton时,大部分情况下,对应的ViewController可以正确捕获到此tap event。但是有时此事件会跳过ViewController,被responder chain里的最后一个responder即AppDelegate处理
一开始,我想依赖responder chain来响应点击,这样就不需要把controller传给view
[closeButton addTarget:nil action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
-(void) closeButtonPressed { [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];// 在LoginViewController上调用dismiss方法 }
不过一开始我设置target的代码写得不对:
YLSRegisterStepOneViewController *step1Controller = [[YLSRegisterStepOneViewController alloc] initWithNibName:nil bundle:nil]; YLSRegisterStepOneView *step1View = [[YLSRegisterStepOneView alloc] initWithFrame:CGRectMake(0, 0, 540, 720) OperType:operType]; step1Controller.view = step1View; step1View.controller = step1Controller;
[closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[closeButton addTarget:nil action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
YLSRegisterStepOneViewController *step1Controller = [[YLSRegisterStepOneViewController alloc] initWithNibName:nil bundle:nil]; step1Controller.view = [[YLSRegisterStepOneView alloc] initWithFrame:CGRectMake(0, 0, 540, 720) OperType:operType Controller:step1Controller];
[closeButton addTarget:controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];