为UILabel添加点击事件

    默认情况下UILabel是不支持点击事件的,网上查了查居然没有一个是完整的答案,现在我提供一个完整的代码。

 

  UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, listV.frame.size.width - 60, listV.frame.size.height)];

l.userInteractionEnabled=YES;
        UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
        
        [l addGestureRecognizer:labelTapGestureRecognizer];


-(void) labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    
   UILabel *label=(UILabel*)recognizer.view;
  
   NSLog(@"%@被点击了",label.text);
    
  
    
}

 

 

 

 
 

你可能感兴趣的:(UILabel)