以编程方式对 UILabel touchUpInside 执行操作?
我有一堆通过代码添加的 UILabel,如果用户的手指触摸内部(很像 IB 中的 UIButton 的 touchUpInside),我想对每个标签执行特定的操作。这样做的最佳方法是什么?
I have a bunch of UILabels that I add through code and I want to perform a specific action for each if the user's finger touches up inside (much like UIButton's touchUpInside in IB). What is the best way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法可能是使用 UIButton 而不是 UILabel。自定义 UIButton 没有边框,看起来像普通标签,但可以为您处理事件跟踪。
否则,您必须从 UILabel 派生并实现 UIResponder 调用以进行触摸处理。
The easiest way is probably to use UIButton instead of UILabel. A custom UIButton has no border and can look like a plain label, but handles the event tracking for you.
Otherwise, you must derive from UILabel and implement the UIResponder calls for touch handling.
您可以使用 UITapGestureRecognizer 对标签字段执行操作。
设置 UITapGestureRecognizer 对象上的点击和触摸次数。
设置标签字段的用户交互,然后
添加目标方法
You can use UITapGestureRecognizer to perform action over the label Fields.
Set the number of taps and touches on the UITapGestureRecognizer object.
set user interaction of label field like
then add target method over the