以后我们会把操作放在视图控制器上进行.我们先建立一个MyButton的类 ,然后在试图控制器上引myButton的头文件,然后在延展部分签署协议形式如下:
@interface MainViewController ()<MyButtonDelegate>
协议步骤
myButton.h文件中
1.声明一份协议
@protocol MyButtonDelegate <NSObject>
//要做的事
-(void)changeColor;
@end
2.设置代理人的属性(在@interface里定义属性)
@property (nonatomic, assign) id<MyButtonDelegate>delegate;
myButton.h文件中
//只要碰就会执行方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//3.设置代理人执行的方法
[self.delegate changeColor];
}
MainViewController.m
4.签协议(在.m的延展部分签署协议)
@interface MainViewController ()<MyButtonDelegate>
5.设置代理人(在实现部分设置)
MyButton *button=[[MyButton alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
button.backgroundColor=[UIColor yellowColor];
[self.view addSubview:button];
[button release];
button.delegate=self;//设置代理人