1 //
2 // BlockButton.h
3 // UIButton-Block
4 //
5 // Created by 大欢 on 16/1/21.
6 // Copyright © 2016年 bjsxt. All rights reserved.
7 //
8
9 #import <UIKit/UIKit.h>
10
11 typedef void(^Block)(UIButton * button);
12
13 @interface BlockButton : UIButton
14
15 @property (nonatomic, copy) Block block;
16
17 @end
1 //
2 // BlockButton.m
3 // UIButton-Block
4 //
5 // Created by 大欢 on 16/1/21.
6 // Copyright © 2016年 bjsxt. All rights reserved.
7 //
8
9 #import "BlockButton.h"
10
11 @implementation BlockButton
12
13 - (instancetype)initWithFrame:(CGRect)frame
14 {
15 self = [super initWithFrame:frame];
16 if (self) {
17
18 [self addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
19 }
20 return self;
21 }
22
23 - (void)doAction:(UIButton *)button {
24
25 self.block(button);
26 }
27
28 @end
1 //
2 // ViewController.m
3 // UIButton-Block
4 //
5 // Created by 大欢 on 16/1/21.
6 // Copyright © 2016年 bjsxt. All rights reserved.
7 //
8
9 #import "ViewController.h"
10 #import "BlockButton.h"
11
12 @interface ViewController ()
13
14 @end
15
16 @implementation ViewController
17
18 - (void)viewDidLoad {
19 [super viewDidLoad];
20
21 BlockButton * btn = [BlockButton buttonWithType:UIButtonTypeCustom];
22 btn.frame = CGRectMake(100, 100, 100, 100);
23 [btn setTitle:@"点我" forState:UIControlStateNormal];
24 [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
25
26 btn.block = ^(UIButton * button) {
27
28 NSLog(@"%@",button);
29
30 };
31 [self.view addSubview:btn];
32
33 }
34
35 - (void)didReceiveMemoryWarning {
36 [super didReceiveMemoryWarning];
37 // Dispose of any resources that can be recreated.
38 }
39
40 @end
![]()