Recognizing multiple UILabels tap for UITapGestureRecogniser

人走茶凉 提交于 2019-12-10 21:00:33

问题


In my view load I have two UILabels and I have added same tapGesture for both.If a particular label is tapped then its functionality should be performed.But I m unable to do so ?

-(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];


    }


}

But when I click on EditProfile label it is going to else block.

How can I recognize which Label is clicked and correspondingly perform required action?


回答1:


Check like this:

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



回答2:


Use Tag format like this. Which will be efficient

-(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];


    }


}


来源:https://stackoverflow.com/questions/14081466/recognizing-multiple-uilabels-tap-for-uitapgesturerecogniser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!