正文:
关于一些标签的检测,看上去冒失没有什么用处,但是,你要仔细想一想,也许发现他非常的棒。。。。。。。。。下面我们就以UILabel为例。。。。。
首先。。我们要创建一个继承与UILabel的类。
代码如下:
#import <UIKit/UIKit.h> @interface Mylabel : UILabel @end
Mylabel的.m文件里写触发事件。。。。。。。代码如下:
<pre name="code" class="objc">#import "Mylabel.h"
@implementation Mylabel
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UIAlertView*al=[[UIAlertView alloc]initWithTitle:nil message:@"wo is label" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
[al show];
}
@end
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
<pre name="code" class="objc">ViewController的.m文件里创建一个UIlabel...但是,我们首先必须导入一个
Mylabel的头文件
#import "ViewController.h"
#import "Mylabel.h"
@interface ViewController ()
{
}
@end
//创建label
Mylabel*kh=[[Mylabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//打开交互功能
kh.userInteractionEnabled=YES;
//设置背景色,容易观察
kh.backgroundColor=[UIColor redColor];
[self.view addSubview:kh];
原文地址:http://blog.csdn.net/zhoushuangjian511/article/details/42076309