Ios ViewController在响应程序链中偶尔会丢失?

Ios ViewController在响应程序链中偶尔会丢失?,ios,objective-c,uiview,uiviewcontroller,Ios,Objective C,Uiview,Uiviewcontroller,今天我遇到了一件非常奇怪的事情:我的自定义ViewController在响应器链中偶尔会丢失 首先,我有一个自定义UIView,其中一个UIButton作为其子视图,然后我像往常一样设置目标操作: [closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 当然,我在自定义UIViewController中实现

今天我遇到了一件非常奇怪的事情:我的自定义ViewController在响应器链中偶尔会丢失

首先,我有一个自定义UIView,其中一个UIButton作为其子视图,然后我像往常一样设置目标操作:

[closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
当然,我在自定义UIViewController中实现closeButtonPressed方法

在大多数情况下,它只是工作得很好。但是有时候,我的ViewController会错过tap事件,最后我发现,tap事件会传递给AppDelegate,并且会调用AppDelegate中的closeButtonPressed方法

我在这个问题上花了一整天,但仍然不知道为什么。你能给我一个线索吗?谢谢。以下是代码,希望对您有所帮助:

初始化并显示我的ViewController和视图的代码:

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。出现此问题时,如果单击UIExtField,然后单击UIButton,UIViewController工作正常

我看不出您在哪里设置要传递到此处的自定义视图的控制器属性:

[closeButton addTarget:*self.controller* action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];

我认为,如果您在视图中创建/设置控制器属性,它将解决问题(除非您已经在这样做,但代码不在这里)。

您将
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添加目标:self.controller操作:@selector(closeButtonPressed)forControlEvents:UIControlEventTouchUpInside]self.controller此处仍然为零

step1View.controller=step1Controller[closeButton addTarget:self.controller操作:@selector(closeButtonPressed)for ControlEvents:UIControlEventTouchUpInside]之后调用code>

更新: 你说响应者链让你困惑,我认为问题在于你分配了视图控制器的视图属性,就像这样
step1Controller.view=step1View这是不推荐的。如果要将自定义视图用作viewcontroller的视图,请执行以下操作:

覆盖viewcontroller中的加载视图方法。 可以替代此方法以手动创建视图。如果选择这样做,请将视图层次结构的根视图指定给视图属性。创建的视图应为唯一实例,不应与任何其他视图控制器对象共享。此方法的自定义实现不应调用super

- (void)loadView
{
    YLSRegisterStepOneView *step1View = [[YLSRegisterStepOneView alloc] initWithFrame:CGRectMake(0, 0, 540, 720) OperType:operType];
    self.view = step1View ;
}

您是否在
YLSRegisterStepOneView
中设置了属性
controller
。在第一个版本中,我没有设置,但由于响应程序链的原因,代码正常工作……然后我发现了此问题,并尝试设置controller属性,但问题仍然存在。最让我困惑的是:它不是每次都会发生,但偶尔,你可以告诉我你是如何设置属性的。对不起,我不知道如何在注释中粘贴代码,所以我会将它们粘贴到第二个答案中,请参见这里。你可以在这里编辑你的问题……嗨,谢谢你的回复。实际上我已经试过你说的了,但是没有用。事实上,当我设置controller属性时,ViewController仍然偶尔会错过事件。让我困惑的是:这种事偶尔会发生,不是每次都会发生哦,是的!我真蠢。我觉得有道理,我会试试你说的。但是我仍然不明白为什么偶尔会发生这种情况,我认为即使目标为零,响应者链机制也应该做些什么…,但至少,也许你指出了解决这个问题的方法是的,现在我在初始化UIView时设置了控制器属性,它工作得很好。谢谢你,朋友。然而,响应链仍然让我感到困惑,我将遵循它