delegate
import
import “MyButton.h”
@implementation MyButton
-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event{
// 3.设置代理人执行的方法
[self.delegate changeColor];
}
@end
import “MainViewController.h”
import “MyButton.h”
// 4.引头文件,签协议
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
MyButton *myButton=[[MyButton alloc] initWithFrame:CGRectMake(100, 100, 175, 40)];
myButton.backgroundColor=[UIColor redColor];
[self.view addSubview:myButton];
[myButton release];
// 5.设置代理人
myButton.delegate=self;
}
// 6.实现方法
-(void)changeColor{
self.view.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}