简体   繁体   中英

ViewController occasionally miss in responder chain?

i meet a very strange thing today: that my custom ViewController occasionally miss in responder chain

first i have a custom UIView, with a UIButton as its subview, then i set the target-action as usual:

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

of course, i implement the closeButtonPressed method in my custom UIViewController

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!

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:

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:

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:

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

additional: aside the UIButton, there is a UITextField. when this problem happens, if i click the UITextField, then click the UIButton, UIViewController works fine

You set controller property of YLSRegisterStepOneView like this:

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?

So [closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside]; self.controller here is still nil.

step1View.controller = step1Controller; is called after [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; which is not recommended. If you want to use your custom view as viewcontroller's view, do this:

Override loadView method in viewcontroller. 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. 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.

- (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:

[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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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