gpt4 book ai didi

iphone - 识别 UITapGestureRecogniser 的多个 UILabels 点击

转载 作者:行者123 更新时间:2023-12-03 19:34:42 25 4
gpt4 key购买 nike

在我的 View 加载中,我有两个 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 标签时,它会被阻止。

如何识别哪个标签被点击并相应地执行所需的操作?

最佳答案

使用这样的标签格式。哪个会更有效率

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


}


}

关于iphone - 识别 UITapGestureRecogniser 的多个 UILabels 点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14081466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com