在UILable上添加点击事件

来源:互联网 发布:Knn算法预测天气预报 编辑:程序博客网 时间:2024/05/21 23:49

朋友问怎么给UILable像button一样做点击事件,顺便写了下,如下 :

第一步,将UILable的userInteractionEnabled值设置为YES,这样才能触发点击事件。

第二步,通过TapGestureRecognize注册事件。

代码如下:

- (void)viewDidLoad

{

    [superviewDidLoad];

    UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(onClickLable)]autorelease];

    [m_lableaddGestureRecognizer:tapGesture];

}

-(void)onClickLable

{

    UIAlertView *alertView = [[[UIAlertViewalloc]initWithTitle:nilmessage:@"fdfdfdf"delegate:nilcancelButtonTitle:@"N"otherButtonTitles:@"Y",nil] autorelease];

    [alertView show];


}