美文网首页iOS开发之常规控件
iOS UILabel 添加点击事件

iOS UILabel 添加点击事件

作者: wandou911 | 来源:发表于2017-12-07 14:24 被阅读1580次

默认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);
   
}

相关文章

网友评论

本文标题:iOS UILabel 添加点击事件

本文链接:https://www.haomeiwen.com/subject/wavdixtx.html