2017-05-09 26 views
0

如何從標籤循環中獲取哪個標籤。我有一個循環中的4個標籤,我想如果我點擊標籤的標籤是3我應該有一個其他標籤出現在視圖上已經隱藏,我怎麼能得到這個?如何通過用戶輕敲手勢獲取標籤的標籤在目標c中

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

更新答案

ü沒有在我們的代碼一些錯誤的座標檢查其標籤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行動

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

     } 
} 
+0

是的,它工作正常用於顯示其中的日誌標籤點擊,但如果我想要得到我隱藏的標籤,當點擊標籤3標籤我怎麼能? – Alex

+0

你可以請更新我的隱藏標籤的答案,然後我會接受並upvote你謝謝。 – Alex

+0

看到它label2最初是隱藏的 – Alex