简体   繁体   English

ViewController偶尔会错过响应者链吗?

[英]ViewController occasionally miss in responder chain?

i meet a very strange thing today: that my custom ViewController occasionally miss in responder chain 我今天遇到一个非常奇怪的事情:我的自定义ViewController有时会在响应者链中丢失

first i have a custom UIView, with a UIButton as its subview, then i set the target-action as usual: 首先,我有一个自定义UIView,将UIButton作为其子视图,然后像往常一样设置target-action:

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

of course, i implement the closeButtonPressed method in my custom UIViewController 当然,我在自定义UIViewController中实现了closeButtonPressed方法

in most time, it just worked fine. 在大多数时间里,它工作得很好。 but occasionally, my ViewController miss the tap event, and finally i found that , the tap event pass to the AppDelegate, and the closeButtonPressed method in AppDelegate is invoked! 但是偶尔,我的ViewController错过了tap事件,最后我发现,tap事件传递给AppDelegate,并调用了AppDelegate中的closeButtonPressed方法!

i spent a whole day in this problem, and still don't know why. 我在这个问题上花了一整天,但仍然不知道为什么。 can you give me a clue? 能给我个提示吗? thanks a lot. 非常感谢。 following is the code, hope it can help: 以下是代码,希望对您有所帮助:

the code to init and present my ViewController and View: 初始化并显示我的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];

the code of the UIView: 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];

the code of the UIViewController: UIViewController的代码:

-(void) closeButtonPressed
{
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];// to dismiss self
}

additional: aside the UIButton, there is a UITextField. 另外:除了UIButton,还有一个UITextField。 when this problem happens, if i click the UITextField, then click the UIButton, UIViewController works fine 当发生此问题时,如果我单击UITextField,然后单击UIButton,则UIViewController可以正常工作

You set controller property of YLSRegisterStepOneView like this: 您可以这样设置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;

You add the button in YLSRegisterStepOneView 's initWithFrame: method, don't you? 您将按钮添加到YLSRegisterStepOneViewinitWithFrame:方法中,不是吗?

So [closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 因此[closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside]; self.controller here is still nil. self.controller仍然为零。

step1View.controller = step1Controller; is called after [closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside]; [closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];之后调用[closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];

updated: You say the responder chain confuses you, I think the problem is that you assign view controller's view property like this step1Controller.view = step1View; 更新:您说响应者链使您感到困惑,我认为问题是您分配了视图控制器的view属性,例如step1Controller.view = step1View; which is not recommended. 不建议这样做。 If you want to use your custom view as viewcontroller's view, do this: 如果要将自定义视图用作viewcontroller的视图,请执行以下操作:

Override loadView method in viewcontroller. 覆盖viewcontroller中的loadView方法。 You can override this method in order to create your views manually. 您可以覆盖此方法,以便手动创建视图。 If you choose to do so, assign the root view of your view hierarchy to the view property. 如果选择这样做,请将视图层次结构的根视图分配给view属性。 The views you create should be unique instances and should not be shared with any other view controller object. 您创建的视图应该是唯一的实例,并且不应与任何其他视图控制器对象共享。 Your custom implementation of this method should not call super. 您对此方法的自定义实现不应调用super。

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

I don't see where you are setting the controller property of the custom view which you are passing here: 我看不到要在此处传递自定义视图的controller属性的位置:

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

I think if you create/set the controller property in your view it would solve the problem (unless you are already doing it but code is not here). 我认为,如果您在视图中创建/设置controller属性,它将解决问题(除非您已经在做,但是代码不在这里)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM