UILabel 添加点击事件

2019-08-24  本文已影响0人  第一梯队利群

默认UILabel 是不支持点击事件的,实现如下:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, listV.frame.size.width - 60, listV.frame.size.height)];
   
label.userInteractionEnabled=YES;
UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
   
[label addGestureRecognizer:labelTapGestureRecognizer];

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

猜你喜欢

热点阅读