[英]ViewController occasionally miss in responder chain?
我今天遇到一个非常奇怪的事情:我的自定义ViewController有时会在响应者链中丢失
首先,我有一个自定义UIView,将UIButton作为其子视图,然后像往常一样设置target-action:
[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
控制器属性:
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;
您将按钮添加到YLSRegisterStepOneView
的initWithFrame:
方法中,不是吗?
因此[closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
self.controller仍然为零。
step1View.controller = step1Controller;
在[closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
之后调用[closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
更新:您说响应者链使您感到困惑,我认为问题是您分配了视图控制器的view属性,例如step1Controller.view = step1View;
不建议这样做。 如果要将自定义视图用作viewcontroller的视图,请执行以下操作:
覆盖viewcontroller中的loadView
方法。 您可以覆盖此方法,以便手动创建视图。 如果选择这样做,请将视图层次结构的根视图分配给view属性。 您创建的视图应该是唯一的实例,并且不应与任何其他视图控制器对象共享。 您对此方法的自定义实现不应调用super。
- (void)loadView
{
YLSRegisterStepOneView *step1View = [[YLSRegisterStepOneView alloc] initWithFrame:CGRectMake(0, 0, 540, 720) OperType:operType];
self.view = step1View ;
}
我看不到要在此处传递自定义视图的controller属性的位置:
[closeButton addTarget:*self.controller* action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
我认为,如果您在视图中创建/设置controller属性,它将解决问题(除非您已经在做,但是代码不在这里)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.