如何检测哪个 UILabel 被点击?

发布于 2024-12-12 21:11:14 字数 1224 浏览 0 评论 0原文

我有三个 UILabel。我想检测哪个标签被点击,然后检索该标签的字符串值。 这就是我正在尝试的方式,我只能设法检测被点击的位置,但我无法检测被点击的标签。

标签创建

for (NSInteger i=1; i<=[pdfs count]; i++){
    UILabel *newLabel=[[UILabel alloc] init];
    newLabel.text = [NSString stringWithFormat:[[pdfs objectAtIndex:(i-1)] lastPathComponent]];
    newLabel.frame = CGRectMake(10, 60*i, 320, 20);
    newLabel.tag=i;
    newLabel.font = [UIFont systemFontOfSize:20.0f];
    newLabel.backgroundColor = [UIColor clearColor];
    newLabel.userInteractionEnabled = YES;
    [self.view addSubview:newLabel];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [newLabel addGestureRecognizer:singleTap]; 
    [newLabel release], newLabel=nil;
    [singleTap release];
} 

检测点击

 - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer

{

CGPoint location;
location = [recognizer locationInView:self.view];

NSString *documentName;
if(location.y<150.0){
    documentName = [[pdfs objectAtIndex:0] lastPathComponent]; 
}
else{
    documentName = [[pdfs objectAtIndex:1] lastPathComponent]; 
}

I have three UILabels. I want to detect which label is Tapped, and then retrieve the string value of that label.
this is how I am trying, I could only manage to detect tapped position, But I couldn't detect which label was tapped.

Label Creation

for (NSInteger i=1; i<=[pdfs count]; i++){
    UILabel *newLabel=[[UILabel alloc] init];
    newLabel.text = [NSString stringWithFormat:[[pdfs objectAtIndex:(i-1)] lastPathComponent]];
    newLabel.frame = CGRectMake(10, 60*i, 320, 20);
    newLabel.tag=i;
    newLabel.font = [UIFont systemFontOfSize:20.0f];
    newLabel.backgroundColor = [UIColor clearColor];
    newLabel.userInteractionEnabled = YES;
    [self.view addSubview:newLabel];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [newLabel addGestureRecognizer:singleTap]; 
    [newLabel release], newLabel=nil;
    [singleTap release];
} 

Detect Taps

 - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer

{

CGPoint location;
location = [recognizer locationInView:self.view];

NSString *documentName;
if(location.y<150.0){
    documentName = [[pdfs objectAtIndex:0] lastPathComponent]; 
}
else{
    documentName = [[pdfs objectAtIndex:1] lastPathComponent]; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

记忆で 2024-12-19 21:11:18

手势识别器知道它属于哪个视图。

UIView *theView = recognizer.view;
// cast it to UILabel if you are sure it is one
UILabel *theLabel = (UILabel *)theView;

A gesture recognizer knows what view it belongs to.

UIView *theView = recognizer.view;
// cast it to UILabel if you are sure it is one
UILabel *theLabel = (UILabel *)theView;
无人问我粥可暖 2024-12-19 21:11:18

为什么要使用标签作为按钮?只需使用按钮即可,它们可以配置为看起来像标签一样。

Why do you want to use labels as buttons? Just use buttons instead, they can be configured to look just like labels.

酒儿 2024-12-19 21:11:17

正如您在标签上添加 GestureRecognizer

 // called when touch is began or when user touches 
    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    {
          UITouch *touch = [touches anyObject]; 

          UILabel *theLabel = (UILabel *)touch.view;

          if (theLabel.tag == 1)
          {}
          else if ...
    }

As you have add GestureRecognizer on label

 // called when touch is began or when user touches 
    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    {
          UITouch *touch = [touches anyObject]; 

          UILabel *theLabel = (UILabel *)touch.view;

          if (theLabel.tag == 1)
          {}
          else if ...
    }
洒一地阳光 2024-12-19 21:11:17

UIGestureRecognizer 有一个对其所附加视图的引用,因此您可以从中获取标签的标签:

int touchedtag = recognizer.view.tag;
documentName = [[pdfs objectAtIndex:touchedtag-1] lastPathComponent]; 

UIGestureRecognizer has a reference to a view it is attached to, so you can get your label's tag from it:

int touchedtag = recognizer.view.tag;
documentName = [[pdfs objectAtIndex:touchedtag-1] lastPathComponent]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文