按钮响应事件
#import "MangoView.h"
//引入头文件#import "mainViewController.h"
@implementation MangoView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self costom];
}
return self;
}
- (void)costom{
UIView *aView = [[UIView alloc]initWithFrame:self.frame];
aView.backgroundColor = [UIColor greenColor];
[self addSubview:aView];
[aView release];
UIView *bView = [[UIView alloc]initWithFrame:CGRectMake(30, 40, 330, 250)];
bView.backgroundColor = [UIColor redColor];
[self addSubview:bView];
[bView release];
UIView *eView = [[UIView alloc]initWithFrame:CGRectMake(30, 330, 330, 300)];
eView.backgroundColor = [UIColor yellowColor];
[self addSubview:eView];
[eView release];
UIView *cView = [[UIView alloc]initWithFrame:CGRectMake(40, 40, 250, 100)];
cView.backgroundColor = [UIColor lightGrayColor];
[eView addSubview:cView];
[cView release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(80, 30, 80, 40);
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"button" forState:UIControlStateNormal];
[button setTitle:@"" forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(bun) forControlEvents:UIControlEventTouchUpInside];
[cView addSubview:button];
UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(40, 160, 250, 100)];
dView.backgroundColor = [UIColor lightGrayColor];
// userInteractionEnabled用户交互是否可用,YES可,NO不可
// dView.userInteractionEnabled = NO;
[eView addSubview:dView];
[dView release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(80, 40, 80, 40);
[btn setTitle:@"Touch" forState:UIControlStateNormal];
[btn setTitle:@"" forState:UIControlStateHighlighted];
btn.backgroundColor = [UIColor brownColor];
[btn addTarget:self action:@selector(touch) forControlEvents:UIControlEventTouchUpInside];
[dView addSubview:btn];
}
- (void)touch{
NSLog(@"touch");
}
- (void)bun{
NSLog(@"button");
}
#import "mainViewController.h"
//引入头文件
#import "MangoView.h"
@interface mainViewController ()
@end
@implementation mainViewController
- (void)loadView{
[super loadView];
self.view = [[MangoView alloc]initWithFrame:self.view.frame];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end