在根视图上实现以下功能:
通过MyButton 实现 button的点击效果
首先建立一个类:MyButton这个类
原代码:
-(void)addNewTarget:(id)target Action:(SEL)action;
target :目标 ,button 执行哪一个类的方法,对应的目标就是那个类的对象
action:动作 ,让button具体做什么事,执行的方法就是对相应的动作
原代码:
@property(nonatomic, assign)id target;
@property(nonatomic ,assign)SEL action;
原代码:
-(void)addNewTarget:(id)target Action:(SEL)action{
_action=action;
_target=target;
}
-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event{
}
[self.target performSelectorInBackground:self.action withObject:self];
原代码:
MyButton *button =[[MyButton alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];
button.layer.cornerRadius=10;
button.layer.borderWidth =1;
button.tag =1000;
[self.view addSubview:button];
[button release];
原代码:
[button addNewTarget:self Action:@selector(click:)];
原代码:
-(void)click:(MyButton *)button{
NSLog(@"实现点击效果");
注意:要引入头文件 ,在appDelegate.m文件中引入根视图控制器头文件;在根视图控制器的.m文件中引入MyButton的头文件
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/mltianya/article/details/47282213