ios - ViewController 偶尔会错过响应链?

标签 ios objective-c uiview uiviewcontroller

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

首先我有一个自定义的 UIView,带有一个 UIButton 作为它的 subview ,然后我像往常一样设置目标操作:

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

当然,我在我的自定义 UIViewController 中实现了 closeButtonPressed 方法

在大多数情况下,它运行得很好。但偶尔,我的 ViewController 会错过点击事件,最后我发现,点击事件传递给 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 属性:

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;

您在 YLSRegisterStepOneViewinitWithFrame: 方法中添加了按钮,不是吗?

所以 [closeButton addTarget:self.controller action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 这里的 self.controller 仍然是 nil。

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

更新: 你说响应者链让你感到困惑,我认为问题是你像这样分配 View Controller 的 View 属性 step1Controller.view = step1View; 这是不推荐的。如果您想将自定义 View 用作 View Controller 的 View ,请执行以下操作:

重写 viewcontroller 中的 loadView 方法。 您可以覆盖此方法以手动创建 View 。如果您选择这样做,请将 View 层次结构的 Root View 分配给 View 属性。您创建的 View 应该是唯一的实例,不应与任何其他 View Controller 对象共享。您对此方法的自定义实现不应调用 super。

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

关于ios - ViewController 偶尔会错过响应链?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20895449/

相关文章:

ios - 如何用图像替换 UIView?

ios - 指针缺少可空性类型说明符

ios - NSURLConnection 在它应该之前调用 connectionDidFinishLoading?

iphone - 核心图 - 禁用某些刻度的轴标签

objective-c - 实现一堆可以改变大小的 View

ios - 扩展 EXT_shader_texture_lod 不适用于 OpenGL ES 3.0 iOS

iphone - Google map 着色 - iOS

ios - 按钮无法快速点击

ios - CGAffineTransformScale 不重置

iOS 如何用另一种语言显示日期,