点击标签时返回标签 [英] Return tag of the label when it it tapped

查看:99
本文介绍了点击标签时返回标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图中有5个标签,分别标记为1,2,3,4和5.我在其上启用了用户互动,并添加了一个点按手势。

I have 5 labels in my view, respectively tagged 1, 2, 3, 4 and 5. I have enabled user interaction on them, and added a tap gesture.

现在我想要的是获取标签的标签。

Now what I want is to get the tag of the label being touched.

我正在做这样的事情:

tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureSelector)];

tapGesture.numberOfTapsRequired = 1.0;     

- (void)tapGestureSelector :(id)sender
{
    // I need the tag to perform different tasks.
    // So that I would like to get the touched label's tag here.    
}

如果我不清楚我的问题,请问我。

If I am not clear in my question please ask me.

感谢您的帮助。

推荐答案

我以这种方式找到解决方案它对我很有用。我希望它也能帮到你。它非常简单和简短。

I find the solution in this way and it worked for me very fine. I hope that it will also help you. It is very simple and short.

我们可以通过将此函数添加到.m文件中来获取标签的标记。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    UILabel *label=(UILabel *)touch.view;

    NSLog(@"Label that is tapped has tag %d",label.tag);
}

再次感谢您提供的所有非常好的建议和答案。我希望将来我总能得到所有问题的好答案。再次感谢所有人。

Thanks again for all of your very nice suggestions and answers. I hope I will always get good answers of my all questions from SO in future. Thanks to all again.

这篇关于点击标签时返回标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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