处理UILabel中的触摸事件并将其挂接到IBAction [英] Handling Touch Event in UILabel and hooking it up to an IBAction

查看:101
本文介绍了处理UILabel中的触摸事件并将其挂接到IBAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在界面构建器中创建了一个 UILabel ,它显示了一些默认文本tap to begin。

Ok, so I have a UILabel created in interface builder that displays some some default text of "tap to begin".

当用户点击 UILabel 时,我希望它触发IBAction方法:
- (IBAction)next; 更新标签上的文字以说出新内容。

如果允许我简单地拖动连接,这将非常方便我的方法到我的标签,然后选择内部修饰,如同一个按钮。但是,唉,没有雪茄。

When the user taps the UILabel I want it to trigger an IBAction method: -(IBAction)next; which updates the text on the label to say something new.
It would be really convenient if this allowed me to simply drag a connection from my method to my label and then select touch up inside, as with a button. but alas, no cigar.

所以无论如何,我想我的问题是,我是否必须继承 UILabel 以使其工作?或者是否有某种方法可以在标签上拖动按钮,但使其为0%不透明。或者是否有一个我想念的更简单的解决方案?

so anyways, I guess my question is, am I going to have to subclass UILabel to get this to work? Or is there some way I can drag a button over the label, but make it 0% opaque. Or is there a simpler solution I'm missing?

推荐答案

检查出来:

UILabel *label = ...
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
      [[UITapGestureRecognizer alloc] initWithTarget:self 
                                              action:@selector(labelTap)];
[label addGestureRecognizer:tapGesture];

诀窍是启用用户互动。

这篇关于处理UILabel中的触摸事件并将其挂接到IBAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆