UIButton的两种block传值方式
UIButton的两种block传值方式


方式1 - 作为属性来传值
BlockView.h 与 BlockView.m
//
// BlockView.h
// Block
//
// Created by YouXianMing on 15/1/14.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
@class BlockView; /**
定义枚举值
*/
typedef enum : NSUInteger {
LEFT_BUTTON = 0x19871220,
RIGHT_BUTTON,
} BUTTON_FLAG; /**
* 定义block
*
* @param flag 枚举值
* @param blockView 当前的blockView
*/
typedef void (^ButtonEvent)(BUTTON_FLAG flag, BlockView *blockView); @interface BlockView : UIView @property (nonatomic, copy) ButtonEvent buttonEvent; // 作为属性的block @property (nonatomic, strong) NSString *leftTitle;
@property (nonatomic, strong) NSString *rightTitle; @end
//
// BlockView.m
// Block
//
// Created by YouXianMing on 15/1/14.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "BlockView.h" @interface BlockView () @property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton; @end @implementation BlockView - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) { // 获取尺寸相关内容
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
CGFloat buttonWidth = width / .f; // 初始化按钮
self.leftButton = [[UIButton alloc] initWithFrame:CGRectMake(, , buttonWidth, height)];
self.leftButton.tag = LEFT_BUTTON;
[self.leftButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.leftButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[self.leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
[self.leftButton addTarget:self
action:@selector(buttonEvents:)
forControlEvents:UIControlEventTouchUpInside];
self.leftButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
[self addSubview:self.leftButton]; self.rightButton = [[UIButton alloc] initWithFrame:CGRectMake(buttonWidth, , buttonWidth, height)];
self.rightButton.tag = RIGHT_BUTTON;
[self.rightButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.rightButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[self.rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
[self.rightButton addTarget:self
action:@selector(buttonEvents:)
forControlEvents:UIControlEventTouchUpInside];
self.rightButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
[self addSubview:self.rightButton]; }
return self;
} - (void)buttonEvents:(UIButton *)button {
// 如果有block值,则从block获取值
if (self.buttonEvent) {
self.buttonEvent(button.tag, self);
}
} #pragma mark - 重写setter,getter方法
@synthesize leftTitle = _leftTitle;
- (void)setLeftTitle:(NSString *)leftTitle {
_leftTitle = leftTitle;
[self.leftButton setTitle:leftTitle forState:UIControlStateNormal];
}
- (NSString *)leftTitle {
return _leftTitle;
} @synthesize rightTitle = _rightTitle;
- (void)setRightTitle:(NSString *)rightTitle {
_rightTitle = rightTitle;
[self.rightButton setTitle:rightTitle forState:UIControlStateNormal];
}
- (NSString *)rightTitle {
return _rightTitle;
} @end
控制器源码:
//
// ViewController.m
// Block
//
// Created by YouXianMing on 15/1/14.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "BlockView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; BlockView *blockView = [[BlockView alloc] initWithFrame:CGRectMake(, , , )];
blockView.layer.borderWidth = .f;
blockView.leftTitle = @"YouXianMing";
blockView.rightTitle = @"NoZuoNoDie"; // 从block中获取到事件
blockView.buttonEvent = ^(BUTTON_FLAG flag, BlockView *blockView) {
NSLog(@"%lu", flag);
}; blockView.center = self.view.center; [self.view addSubview:blockView];
} @end

方式2 - 作为方法来传值
BlockView.h 与 BlockView.m
//
// BlockView.h
// Block
//
// Created by YouXianMing on 15/1/14.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
@class BlockView; /**
定义枚举值
*/
typedef enum : NSUInteger {
LEFT_BUTTON = 0x19871220,
RIGHT_BUTTON,
} BUTTON_FLAG; /**
* 定义block
*
* @param flag 枚举值
* @param blockView 当前的blockView
*/
typedef void (^ButtonEvent)(BUTTON_FLAG flag, BlockView *blockView); @interface BlockView : UIView @property (nonatomic, strong) NSString *leftTitle;
@property (nonatomic, strong) NSString *rightTitle; /**
* 定义成方法来实现
*
* @param buttonEvent block
*/
- (void)buttonEvent:(ButtonEvent)buttonEvent; @end
//
// BlockView.m
// Block
//
// Created by YouXianMing on 15/1/14.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "BlockView.h" @interface BlockView () @property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, copy) ButtonEvent buttonEvent; @end @implementation BlockView - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) { // 获取尺寸相关内容
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
CGFloat buttonWidth = width / .f; // 初始化按钮
self.leftButton = [[UIButton alloc] initWithFrame:CGRectMake(, , buttonWidth, height)];
self.leftButton.tag = LEFT_BUTTON;
[self.leftButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.leftButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[self.leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
[self.leftButton addTarget:self
action:@selector(buttonEvents:)
forControlEvents:UIControlEventTouchUpInside];
self.leftButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
[self addSubview:self.leftButton]; self.rightButton = [[UIButton alloc] initWithFrame:CGRectMake(buttonWidth, , buttonWidth, height)];
self.rightButton.tag = RIGHT_BUTTON;
[self.rightButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.rightButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[self.rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
[self.rightButton addTarget:self
action:@selector(buttonEvents:)
forControlEvents:UIControlEventTouchUpInside];
self.rightButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
[self addSubview:self.rightButton];
}
return self;
} - (void)buttonEvents:(UIButton *)button {
if (self.buttonEvent) {
self.buttonEvent(button.tag, self);
}
} - (void)buttonEvent:(ButtonEvent)buttonEvent {
// 初始化block
self.buttonEvent = ^(BUTTON_FLAG flag, BlockView *blockView) {
if (buttonEvent) {
buttonEvent(flag, blockView);
}
};
} #pragma mark - 重写setter,getter方法
@synthesize leftTitle = _leftTitle;
- (void)setLeftTitle:(NSString *)leftTitle {
_leftTitle = leftTitle;
[self.leftButton setTitle:leftTitle forState:UIControlStateNormal];
}
- (NSString *)leftTitle {
return _leftTitle;
} @synthesize rightTitle = _rightTitle;
- (void)setRightTitle:(NSString *)rightTitle {
_rightTitle = rightTitle;
[self.rightButton setTitle:rightTitle forState:UIControlStateNormal];
}
- (NSString *)rightTitle {
return _rightTitle;
} @end
控制器源码:
//
// ViewController.m
// Block
//
// Created by YouXianMing on 15/1/14.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "BlockView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; BlockView *blockView = [[BlockView alloc] initWithFrame:CGRectMake(, , , )];
blockView.layer.borderWidth = .f;
blockView.leftTitle = @"YouXianMing";
blockView.rightTitle = @"NoZuoNoDie"; [blockView buttonEvent:^(BUTTON_FLAG flag, BlockView *blockView) {
NSLog(@"%lu", flag);
}]; blockView.center = self.view.center; [self.view addSubview:blockView];
} @end

UIButton的两种block传值方式的更多相关文章
- Windows Azure VM的两种shut down 方式
今天在调查Azure的价格时,发现下面的语句,来自http://azure.microsoft.com/en-us/pricing/details/virtual-machines/ * If my ...
- 两种隐藏元素方式【display: none】和【visibility: hidden】的区别
此随笔的灵感来源于上周的一个面试,在谈到隐藏元素的时候,面试官突然问我[display: none]和[visibility: hidden]的区别,我当时一愣,这俩有区别吗,好像有,但是忘记了啊,因 ...
- Linux共享库两种加载方式简述
Linux共享库两种加载方式简述 动态库技术通常能减少程序的大小,节省空间,提高效率,具有很高的灵活性,对于升级软件版本也更加容易.与静态库不同,动态库里面的函数不是执行程序本身 的一部分,而是 ...
- android环境下两种md5加密方式
在平时开发过程中,MD5加密是一个比较常用的算法,最常见的使用场景就是在帐号注册时,用户输入的密码经md5加密后,传输至服务器保存起来.虽然md5加密经常用,但是md5的加密原理我还真说不上来,对md ...
- Form表单中method=post/get两种数据传输的方式的区别
Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响.虽然为了方便的得到变量值,Web容器已经屏蔽了二者的一 ...
- 两种数据传输的方式——get和post。
Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响.虽然为了方便的得到变量值,Web容器已经屏蔽了二者的一 ...
- Xamarin Android Fragment的两种加载方式
android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...
- nginx 和 tp兼容pathinfo和rewrite两种url访问方式
环境:centos7,yum安装的nginx1.10.php-fpm,tp3.2 本方法只需要配置nginx.conf的一个文件就可以支持pathinfo和rewrite两种url访问方式 vim / ...
- mybatis中两种取值方式?谈谈Spring框架理解?
1.mybatis中两种取值方式? 回答:Mybatis中取值方式有几种?各自区别是什么? Mybatis取值方式就是说在Mapper文件中获取service传过来的值的方法,总共有两种方式,通过 $ ...
随机推荐
- java获得当前日期是今年的第几周,以及这周的开始日期的方法
直接上代码,备份使用 时间戳,长整型存储 long startTime1 = 1530613938532l; Calendar cale ...
- tomcat启动(五)Catalina分析-service.init
上篇写到StandardService.init() 这个方法做什么呢?一起来看看. 这个类也是实现了Lifecycle 如图.这个图中i表示Interface接口.如Lifecycle,Contai ...
- LogStash启动报错:<Redis::CommandError: ERR unknown command 'script'>与batch_count 的 配置
环境条件: 系统版本:centos 6.8 logstash版本:6.3.2 redis版本:2.4 logstash input配置: input { redis { host => &qu ...
- Ethereum 源码分析之框架
accounts 实现了一个高等级的以太坊账户管理 bmt 二进制的默克尔树的实现 build 主要是编译和构建的一些脚本和配置 cmd ...
- php发送post请求的三种方法
引用:http://blog.sjzycxx.cn/post/435/ 1.使用 file_get_contents() /** * 发送post请求 * @param string $url 请求地 ...
- redis集群与分片(2)-Redis Cluster集群的搭建与实践
Redis Cluster集群 一.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper等,但从redis 3.0之后版本支持redis-cluster集群,Re ...
- Chrome 谷歌如何快速实现跨域
第一步:在你的E盘或者其他盘新建一个文件夹,命名为:E:\MyChromeDevUserData 第二步:找到你的谷歌浏览器快捷图标,鼠标右键选择属性,出现以下界面: 第三步:在目标选项的最后添加: ...
- jQuery+Ajax+PHP 制作简单的异步数据传输(测试用户名是否可用)
实现基本异步数据传输,略去与数据库交换,先直接在PHP端判断:用户名为 user1 即为不可用, 测试时外加了 普遍的 “Loading..." 功能,此功能可直接在PHP中循环延时 for ...
- easyUi datagrid鼠标经过提示单元格内容
此文章是基于 EasyUI+Knockout实现经典表单的查看.编辑 一. jquery.cellTip.js /** * 扩展两个方法 */ using('datagrid', function() ...
- bzoj1061 NOI2018 志愿者招募——solution
Description 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难 题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能 ...