为UILabel添加点击事件
程序员文章站
2022-03-18 12:04:54
...
默认情况下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); }
有任何问题请联系微信
如果您觉得我的文章给了您帮助,请为我买一杯饮料吧!以下是我的支付宝,意思一下我将非常感激!