如何在目标c中使用用户点击手势获取标签标签

时间:2017-05-09 09:47:11

标签: objective-c uilabel uitapgesturerecognizer

如何从标签循环中获取哪个标签。我在循环中有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;

}

1 个答案:

答案 0 :(得分:1)

更新回答

你在你的代码中做了一些错误的坐标检查标签2:

PostgreSQL81Dialect

// UITapGestureRecognizer操作

PostgreSQLDialect