自定义 UIControl 始终发送 nil UIEvent 对象
我有一个派生自 UIControl 的类,在其中监视 TouchBegan/Moved/Ended/Cancelled 事件。在这些事件中,我在超级响应者和下一个响应者上调用相同的事件,如下所示:
[self.nextResponder touchesEnded:touches withEvent:event];
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
[super touchesEnded:touches withEvent:event];
我有一个拥有表视图的父控件。表视图中的每个单元格都包含我的自定义控件之一。为了响应单击,我在创建控件并将其添加到表视图单元格时为其分配一个选择器,如下所示:
[myControl addTarget:self action:@selector(myControlTap:withEvent:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:myControl];
父控件的选择器如下所示:
- (void)myControlTap:(id)sender withEvent:(UIEvent *)event{
//UIEvent is always null!
}
有人能告诉我为什么 UIEvent 始终为零吗?如果我使用 UIButton 的实例而不是自定义 UIControl,则 UIEvent 不为零。此外,如果我不在控件中调用 sendActionsForControlEvents ,那么父控件将永远不会收到任何事件通知。
I have a class that derives from UIControl in which I monitor the touchesBegan/Moved/Ended/Cancelled events. Within those events I call the same events on both super and next responder like so:
[self.nextResponder touchesEnded:touches withEvent:event];
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
[super touchesEnded:touches withEvent:event];
I have a parent control that owns a table view. The cells in the table view each contain one of my custom controls. In order to respond to a click I assign a selector to the control when I am creating it and adding it to the table view cell like so:
[myControl addTarget:self action:@selector(myControlTap:withEvent:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:myControl];
The parent control's selector looks like this:
- (void)myControlTap:(id)sender withEvent:(UIEvent *)event{
//UIEvent is always null!
}
Can someone tell me why UIEvent is always nil? If I use an instance of UIButton instead of my custom UIControl then UIEvent is not nil. Furthermore, if I don't call sendActionsForControlEvents in my control then the parent control will never receive any event notifications.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在方法和选择器中将“withEvent”更改为“forEvent”。
Change "withEvent" to "forEvent" in the method and the selector.