在我的视图加载中我有两个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块.
如何识别单击哪个标签并相应执行所需操作?
解决方法
像这样检查:
if(currentLabel == lblEditProfile) //code here else //code here