ios 处理 UILabel 中的触摸事件并将其连接到 IBAction
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3169798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling Touch Event in UILabel and hooking it up to an IBAction
提问by wfbarksdale
Ok, so I have a UILabel
created in interface builder that displays some some default text of "tap to begin".
好了,我有一个UILabel
接口创建生成器,显示“点击开始”的一些一些默认的文本。
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
我希望它触发一个 IBAction 方法时:
-(IBAction)next;
它更新标签上的文本以说出新的内容。
如果这允许我简单地将连接从我的方法拖到我的标签,然后在里面选择 touch up,这将非常方便,就像使用按钮一样。但唉,没有雪茄。
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
得到这个工作?或者有什么方法可以将按钮拖到标签上,但使其为 0% 不透明。或者是否有我缺少的更简单的解决方案?
回答by Scott Persinger
Check it out:
一探究竟:
UILabel *label = ...
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(labelTap)];
[label addGestureRecognizer:tapGesture];
The trick is to enable user interaction.
诀窍是启用用户交互。
回答by Remover
UILabel inherits from UIView which inherits from UIResponder. All UIresponder objects can handle touch events. So in your class file which knows about your view (which contains the UIlabel) implement:
UILabel 继承自 UIView,后者继承自 UIResponder。所有 UIresponder 对象都可以处理触摸事件。因此,在了解您的视图(包含 UIlabel)的类文件中,实现:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
In interface builder set the UILabel's tag value. when touches occur in your touchesBegan method, check the tag value of the view to which the tag belongs:
在界面构建器中设置 UILabel 的标签值。当 touchesBegan 方法中发生触摸时,检查标签所属视图的标签值:
UITouch *touch = [touches anyObject];
if(touch.view.tag == MY_TAG_VAL)
label.text = @"new text";
You connect your code in your class file with the UILabel object in interface builder by declaring your UILabel instance variable with the IBOutlet prefix:
您可以通过使用 IBOutlet 前缀声明 UILabel 实例变量,将类文件中的代码与界面构建器中的 UILabel 对象连接起来:
IBOutlet UILabel *label;
Then in interface builder you can connect them up.
然后在界面构建器中,您可以将它们连接起来。
回答by Eiko
You can use a UIButton, make it transparent, i.e. custom type without an image, and add a UILabel on it (centered). Then wire up the normal button events.
您可以使用 UIButton,使其透明,即没有图像的自定义类型,并在其上添加 UILabel(居中)。然后连接正常的按钮事件。
回答by César Cruz
Swift 3
斯威夫特 3
You have an IBOutlet
你有一个IBOutlet
@IBOutlet var label: UILabel!
In which you enable user interaction and add a gesture recognizer
在其中启用用户交互并添加手势识别器
label.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(userDidTapLabel(tapGestureRecognizer:)))
label.addGestureRecognizer(tapGesture)
And finally, handle the tap
最后,处理水龙头
func userDidTapLabel(tapGestureRecognizer: UITapGestureRecognizer) {
// Your code goes here
}