Target-Action
import
import “MyButton.h”
@implementation MyButton
// 3.自定义的方法实现
-(void)addnewTarget:(id)target Action:(SEL)action{
self.target=target;
self.action=action;
}
// 4.设置一个点击的触发条件
-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event{
// 5.让myButton来完成委托人的人物,从而实现点击效果
[self.target performSelector:self.action withObject:self];
}
import “MainViewController.h”
import “MyButton.h”
@interface MainViewController ()
@end
@implementation MainViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.// 通过UIView来模拟一个button点击
MyButton *myButton=[[MyButton alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
myButton.backgroundColor=[UIColor redColor];
[self.view addSubview:myButton];
[myButton release];// 6.使用自定义的方法
[myButton addnewTarget:self Action:@selector(click:)];
}
-(void)click:(MyButton *)button{
NSLog(@”实现点击效果”);
}