subView的touchesBegan:withEvent:选择器未调用
在下面的代码中,我创建了一个 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 UIView
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不需要创建一个类别并添加
touchesBegan
: 来完成这项工作。您是否确保label.userInteractionEnabled = YES;
?I don't think you need to create a category and add
touchesBegan
: to make this work. Have you ensured thatlabel.userInteractionEnabled = YES;
?