UIButton无法响应'touchUpInside'事件,但对'touchDown'事件有响应

3
在我的程序中,我已经通过一个 UIVIewController 的属性以编程方式声明了一个 UIButton 。 我已经设置它在用户触发 touchUpInside 事件时发送动作,如下所示: [restartButton addTarget:self action:@selector(handleRestart) forControlEvents:UIControlEventTouchUpInside]; 其中restartButtonUIButton的名称。
当我将 UIControlEventTouchDown 输入到 forControlEvents 参数中时,它可以正常工作。 但是,UIControlEventTouchUpInside 根本不起作用。 有人知道这是为什么吗?

你是否意外覆盖了 touchesMovedtouchesEnded 方法? - Rok Jarc
不是,但如果有办法使其工作,这种方法比那种方法更可取。 - Fitzy
你应该尝试使用 UIControlEventTouchUpInside 在正常方式下让它工作。我只是问一下,因为那样(覆盖)可能是原因。 - Rok Jarc
2个回答

4

3

您可能需要检查restartButton.bounds。您可能只是将其设置为零大小,导致UIControlEventTouchUpInside从未发生。最好的检查方法是向图层添加边框。

[restartButton.layer setBorderWidth:1.0f];
[restartButton.layer setBorderColor:[UIColor redColor].CGColor];

不要忘记 #import <QuartzCore/QuartzCore.h>,否则上述内容将无法正常工作。


我刚刚试过了,尺寸不是0。边框完美地适配在按钮周围。还有其他想法吗? - Fitzy
你尝试过TouchUpOutside事件吗? - Pierre-Luc Simard
这是我的问题。谢谢你的建议。 - Lereveme

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接