为 UITapGestureRecognizer 识别多个 UILabel 点击

Posted

技术标签:

【中文标题】为 UITapGestureRecognizer 识别多个 UILabel 点击【英文标题】:Recognizing multiple UILabels tap for UITapGestureRecogniser 【发布时间】:2012-12-29 12:03:59 【问题描述】:

在我的视图加载中,我有两个 UILabel,并且我为两者添加了相同的 tapGesture。如果点击特定标签,则应该执行其功能。但我无法这样做?

-(void)viewDidLoad
  lblEditProfile.userInteractionEnabled = YES;
  UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClicked:)];
    [tapGestureRecognizer setNumberOfTapsRequired:1];
   [lblEditProfile addGestureRecognizer:tapGestureRecognizer];
   [tapGestureRecognizer release];

   lblViewDetails.userInteractionEnabled = YES;
  tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClicked:)];
    [tapGestureRecognizer setNumberOfTapsRequired:1];

    [lblViewDetails addGestureRecognizer:tapGestureRecognizer];
    [tapGestureRecognizer release];


-(IBAction)labelClicked:(UITapGestureRecognizer*)tapGestureRecognizer


    currentLabel = (UILabel *)tapGestureRecognizer.view;
    NSLog(@"tap %@",tapGestureRecognizer.view);

    if(currentLabel.text==@"Edit Profile")
    


        UserProfile *userProfile = [[UserProfile alloc] initWithNibName:@"UserProfile" bundle:nil];
        userProfile.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:userProfile animated:YES];
        [userProfile release];



    
    else
    

        ViewDetails *viewDetails = [[ViewDetails alloc] initWithNibName:@"UserAppointments" bundle:nil];
        viewDetails.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController: viewDetails animated:YES];
        [viewDetails release];


    



但是当我点击 EditProfile 标签时,它会阻塞。

如何识别点击了哪个标签并相应地执行所需的操作?

【问题讨论】:

为每个标签设置标签并根据标签值进行比较。 【参考方案1】:

使用这样的标签格式。这将是有效的

-(void)viewDidLoad
  lblEditProfile.userInteractionEnabled = YES;
  lblEditProfile.tag = 1;
  UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClicked:)];
    [tapGestureRecognizer setNumberOfTapsRequired:1];
   [lblEditProfile addGestureRecognizer:tapGestureRecognizer];
   [tapGestureRecognizer release];

   lblViewDetails.userInteractionEnabled = YES;
   lblViewDetails.tag = 2;
  tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClicked:)];
    [tapGestureRecognizer setNumberOfTapsRequired:1];

    [lblViewDetails addGestureRecognizer:tapGestureRecognizer];
    [tapGestureRecognizer release];


-(IBAction)labelClicked:(UITapGestureRecognizer*)tapGestureRecognizer


    currentLabel = (UILabel *)tapGestureRecognizer.view;
    NSLog(@"tap %d",tapGestureRecognizer.tag);

    if(currentLabel.tag == 1)
    


        UserProfile *userProfile = [[UserProfile alloc] initWithNibName:@"UserProfile" bundle:nil];
        userProfile.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:userProfile animated:YES];
        [userProfile release];



    
    else if(currentLabel.tag == 2)
    

        ViewDetails *viewDetails = [[ViewDetails alloc] initWithNibName:@"UserAppointments" bundle:nil];
        viewDetails.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController: viewDetails animated:YES];
        [viewDetails release];


    



【讨论】:

这部分:-(IBAction)labelClicked:(UITapGestureRecognizer*)tapGestureRecognizer currentLabel = (UILabel *)tapGestureRecognizer.view;帮助我获得了被触及的标签。谢谢!【参考方案2】:

像这样检查:

  if(currentLabel == lblEditProfile)
    //code here
  else
     //code here

【讨论】:

以上是关于为 UITapGestureRecognizer 识别多个 UILabel 点击的主要内容,如果未能解决你的问题,请参考以下文章

将类设置为 UITapGestureRecognizer 的目标而不是“自我”时应用程序崩溃

为 UIView 添加时,UITapGestureRecognizer 无法正常工作无法检测到问题

UITableview 的背景视图上的 UITapGestureRecognizer 不起作用

UITapGestureRecognizer 错误

在 UITapGestureRecognizer(双击)添加到 UIButton 会减慢将按钮图像设置为选中状态

iOS7:UITapGestureRecognizer 重用问题