【发布时间】:2014-01-03 02:18:20
【问题描述】:
我今天遇到了一件很奇怪的事情:我的自定义 ViewController 偶尔会在响应者链中丢失
首先我有一个自定义 UIView,它的子视图是 UIButton,然后我像往常一样设置目标操作:
[closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
当然,我在自定义 UIViewController 中实现了 closeButtonPressed 方法
在大多数情况下,它运行良好。但是偶尔我的ViewController会错过tap事件,最后我发现,tap事件传给了AppDelegate,调用了AppDelegate中的closeButtonPressed方法!
我花了一整天的时间解决这个问题,但仍然不知道为什么。你能给我一个线索吗?多谢。以下是代码,希望对您有所帮助:
初始化并呈现我的 ViewController 和 View 的代码:
YLSRegisterStepOneViewController *step1Controller = [[YLSRegisterStepOneViewController alloc] initWithNibName:nil bundle:nil];
step1Controller.view = [[YLSRegisterStepOneView alloc] initWithFrame:CGRectMake(0, 0, 540, 720) OperType:operType];
step1Controller.modalPresentationStyle = UIModalPresentationFormSheet;
step1Controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:step1Controller animated:YES completion:nil];
UIView的代码:
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
closeButton.frame = CGRectMake(10, 10, 50, 50);
[closeButton setTitle:NSLocalizedString(@"button_close", @"") forState:UIControlStateNormal];
[closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeButton];
UIViewController的代码:
-(void) closeButtonPressed
{
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];// to dismiss self
}
附加:除了 UIButton,还有一个 UITextField。当这个问题发生时,如果我点击 UITextField,然后点击 UIButton,UIViewController 工作正常
【问题讨论】:
-
您是否在
YLSRegisterStepOneView中设置了属性controller。 -
在第一个版本中,我没有设置,但是由于响应链,代码可以工作......然后我发现这个问题,并尝试设置控制器属性,但问题仍然存在。最让我困惑的是:它不是每次都发生,而是偶然,偶尔发生
-
你能告诉我你是如何设置属性的吗?
-
对不起,我不知道如何在评论中粘贴代码,所以我将它们粘贴在第二个答案中,请看那里
-
您可以在此处编辑您的问题...
标签: ios objective-c uiview uiviewcontroller