UI之responder

按钮响应事件

#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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值