在我的视图中,我有两个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标签时,它会阻止。
如何识别单击哪个标签并相应地执行所需操作?
答案 0 :(得分: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];
}
}
答案 1 :(得分:0)
像这样检查:
if(currentLabel == lblEditProfile)
//code here
else
//code here