UI部分协议的使用方法
@protocol MybuttonDelegate <NSObject>
-(void)changeColor;
@end
@interface MyButton : UIView
@property(nonatomic,assign)id<MybuttonDelegate>delegate;
@end
以下内容为部分实现内容 代理人所需要执行的方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.delegate changeColor];
}
在根视图控制器中,要在.m文件中签订协议
@interface MainViewController ()<MybuttonDelegate>
MyButton *myButton = [[MyButton alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
myButton .backgroundColor = [UIColor yellowColor];
[self.view addSubview:myButton];
[myButton release];
myButton.delegate = self;
实现协议的内容
-(void)changeColor{
self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}