自定義的View,View中有自定義的UILabel,然后UILabel添加了點擊事件沪曙。
action中的方法tap邑蒋,是UITapGestureRecognizer對象姓蜂,通過sender獲取當前的view,然后通過類型轉(zhuǎn)換來獲取UILabel医吊。
代碼如下:
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(taptouch:)];
[label addGestureRecognizer:tap];
label.userInteractionEnabled = YES;
[self addSubview:label];
- (void)taptouch:(UITapGestureRecognizer *)sender
{
UILabel *label = (UILabel *)sender.view;
NSLog(@"label text is %@",label.text);
}