首页 > 移动平台 > 详细

iOS学习-UIButton->Block

时间:2016-01-25 15:02:26      阅读:185      评论:0      收藏:0      [点我收藏+]
 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

技术分享

iOS学习-UIButton->Block

原文:http://www.cnblogs.com/MrWuYindi/p/5157257.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!