subView的touchesBegan:withEvent:选择器未调用

发布于 2024-11-30 12:13:16 字数 1002 浏览 0 评论 0原文

在下面的代码中,我创建了一个 MTLabel 对象和一个 UIButton 对象,更改了它们的框架,然后添加 UIButton 作为 'MTLabel' 的子

MTLabel* label = [MTLabel labelWithTitle:title];
label.frame = ...
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
                action:@selector(didSelectStore:)
      forControlEvents:UIControlEventAllEvents];
[label addSubview:rightButton];
rightButton.frame = ...;
[an addLabel:label];

视图两个 UIView 都出现在屏幕上,但是触摸 rightButton 时永远不会调用 didSelectStore:

由于 MTLabel 是在第三方库中定义的,因此我没有它的源代码。所以我写了一个这样的类别:

@implementation MTLabel(button)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
}
......
@end

这样做之后,仍然没有调用 [rightButton TouchesBegan:event:] ,更不用说那个动作了。

因此触摸事件在该子视图中没有按预期工作。

知道如何发生以及我应该做什么吗?

谢谢。

In the following code, I created a MTLabel object and a UIButton object, changed their frame, then add UIButton as subView of 'MTLabel'

MTLabel* label = [MTLabel labelWithTitle:title];
label.frame = ...
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
                action:@selector(didSelectStore:)
      forControlEvents:UIControlEventAllEvents];
[label addSubview:rightButton];
rightButton.frame = ...;
[an addLabel:label];

After that both UIViews appears on screen, howeverdidSelectStore: is never called when touching that rightButton.

As MTLabel is defined in a third-party lib, I don't have it's source code. So I write a category like this:

@implementation MTLabel(button)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
}
......
@end

After doing that, [rightButton touchesBegan:event:] is still not called, not mention that action.

So the touch events are not working as expected in that subView.

Any idea on how then happen and what should I do?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑忘罢 2024-12-07 12:13:16

我认为您不需要创建一个类别并添加 touchesBegan: 来完成这项工作。您是否确保label.userInteractionEnabled = YES;

I don't think you need to create a category and add touchesBegan: to make this work. Have you ensured that label.userInteractionEnabled = YES;?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文