[英]How to get tag of label with user tap gestures in objective c
How can i get which label is being tapped from a loop of labels. 我如何才能从标签循环中窃听哪个标签。 I have 4 labels in a loop and i want if i click on label whose tag is 3 i should got an other label appear on view which is already hidden, how can i get this? 我在一个循环中有4个标签,如果我单击标签为3的标签,我想让另一个标签出现在已隐藏的视图中,我怎么能得到这个标签?
CGFloat y1 = 100.0;
CGFloat x1 = 30.0;
CGRect rect1 = CGRectMake(x1, y1, 100, 30);
for (int i = 0; i < 4; i++)
{
label = [[UILabel alloc] initWithFrame:rect1];
label.tag = 4+i;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor blueColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
label.text = @"Hello";
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotTapped:)];
[label addGestureRecognizer:tap];
[self.view addSubview:label];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(200, y1, 50, 30)];
label2.tag = 100+i;
label2.textAlignment = NSTextAlignmentCenter;
label2.backgroundColor = [UIColor yellowColor];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
label2.text = @"World";
label2.hidden = YES;
[self.view addSubview:label2];
rect1.origin.y += 45;
}
} }
-(void)gotTapped:(UITapGestureRecognizer*)sender {
for (UILabel *v in label2) {
v.hidden = !v.hidden;
switch(((UITapGestureRecognizer *)sender).view.tag)
{
case 1:
NSLog(@"Clicked on label 1");
break;
case 2:
NSLog(@"Clicked on label 2");
break;
} }
updated answer . 更新的答案 。
u did some wrong co-ordinates in ur code check it for label 2 : 您在您的代码中做了一些错误的坐标检查,是否为标签2:
y1=y1+45;// extra line u need to add
CGFloat y1 = 40;
CGFloat x1 = 30.0;
CGRect rect1 = CGRectMake(x1, y1, 100, 30);
for (int i = 0; i < 4; i++)
{
label = [[UILabel alloc] initWithFrame:rect1];
label.tag = i+1;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor blueColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
label.text = @"Hello";
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotTapped:)];
[label addGestureRecognizer:tap];
[self.view addSubview:label];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(150, y1, 50, 30)];
label2.tag = i+100;
label2.textAlignment = NSTextAlignmentCenter;
label2.backgroundColor = [UIColor redColor];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:@"Verdana-Bold" size:8];
label2.text = [NSString stringWithFormat:@"%@%ld",@"world",(long)label2.tag];
label2.hidden = YES;
[self.view addSubview:label2];
rect1.origin.y += 45;
y1=y1+45;
NSLog(@"tag values of label2 are %ld",label2.tag);
}
// UITapGestureRecognizer actions // UITapGestureRecognizer动作
-(void)gotTapped:(UITapGestureRecognizer*)sender {
NSLog(@"%ld",(((UITapGestureRecognizer *)sender).view.tag));
int num= 99 +(int)(((UITapGestureRecognizer *)sender).view.tag);
NSLog(@"%d",num);
UILabel *label3 = [self.view viewWithTag:num];
label3.hidden = NO;
switch(((UITapGestureRecognizer *)sender).view.tag)
{
case 1:
NSLog(@"Clicked on label 1");
break;
case 2:
NSLog(@"Clicked on label 2");
NSLog(@"%ld",100 +((UITapGestureRecognizer *)sender).view.tag) ;
break;
case 3:
NSLog(@"Clicked on label 3");
NSLog(@"%ld",100 +((UITapGestureRecognizer *)sender).view.tag) ;
break;
case 4:
NSLog(@"Clicked on label 4");
NSLog(@"%ld",100 +((UITapGestureRecognizer *)sender).view.tag) ;
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.