Sindhia
Sindhia

Reputation: 411

Recognizing multiple UILabels tap for UITapGestureRecogniser

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?

Upvotes: 3

Views: 1834

Answers (2)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Check like this:

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

Upvotes: 0

Sumanth
Sumanth

Reputation: 4921

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


    }


}

Upvotes: 7

Related Questions