#import <UIKit/UIKit.h>
@protocol MyButtonDelegate <NSObject>
- (void)changeColor;
@end
@interface MyButton : UIView
@property(nonatomic, assign)id<MyButtonDelegate>delegate;
@end
#import "MyButton.h"
@implementation MyButton
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.delegate changeColor];
}
@end
#import "MainViewController.h"
#import "MyButton.h"
@interface MainViewController ()<MyButtonDelegate>
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(120, 100, 150, 40)];
button.backgroundColor = [UIColor whiteColor];
[self.view addSubview:button];
[button release];
button.delegate = self;
UISwitch *swift = [[UISwitch alloc] initWithFrame:CGRectMake(120, 200, 50, 50)];
swift.backgroundColor = [UIColor grayColor];
[self.view addSubview:swift];
[swift release];
[swift addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)click:(UISwitch *)swift
{
NSLog(@"1");
}
- (void)changeColor
{
self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end