3

ビューロードでは、2つの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ラベルをクリックすると、elseブロックになります。

クリックされたラベルを認識し、それに応じて必要なアクションを実行するにはどうすればよいですか?

4

2 に答える 2

7

このようなタグ形式を使用します。どちらが効率的でしょう

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


    }


}
于 2012-12-29T12:12:13.703 に答える
0

次のように確認してください。

  if(currentLabel == lblEditProfile)
    //code here
  else
     //code here
于 2012-12-29T12:13:41.757 に答える