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使用Gecco爬虫 抓取网页内容(附Demo)
JAVA 爬虫工具有挺多的,但是Gecco是一个挺轻量方便的工具. 先上项目结构图. 这是一个 JAVASE的 MAVEN 项目,要添加包依赖,其他就四个文件.log4j.properties 加上三 ...
- [作业] Python入门基础--三级菜单
用字典存储数据 可以随时返回上一级,随时退出程序 只能用循环判断等内置方法,不得导入模块 menu = { '广东':{ '广州':{ '越秀区':{ '面积':'33.80', '人口':'115万 ...
- 软工网络15-Alpha阶段敏捷冲刺
一.Alpha 阶段全组总任务 二.各个成员在 Alpha 阶段认领的任务 三. 整个项目预期的任务量 四.明日各个成员的任务安排 任务 预计时长 负责人 授权界面 2h 王华俊 难度选择界面 1h ...
- How系列-公网如何ssh到内网的Linux系统中?
起因 最近碰到一件事:B同学在他电脑的Ubuntu虚拟机中学习搭建服务器碰到了问题,要我帮他看下.我总不能一个QQ远程桌面连过去,那样操作会卡到崩溃.ssh过去是最好的方法,不过他的电脑跟我不在一个局 ...
- 设计模式学习--面向对象的5条设计原则之接口隔离原则--ISP
一.ISP简介(ISP--Interface Segregation Principle): 使用多个专门的接口比使用单一的总接口要好.一个类对另外一个类的依赖性应当是建立在最小的接口上的.一个接口代 ...
- jQuery源码分析-03构造jQuery对象-源码结构和核心函数
3. 构造jQuery对象 3.1源码结构 先看看总体结构,再做分解: (function( window, undefined ) { var jQuery = (function() { // 构 ...
- catch异常
int ret = -1; try { ret = tBuyerCodeApplyInfoService.insertTBuyerCodeApplyInfoBySelective(buyerCode) ...
- 打造自己的LinQProvider(四)
打造自己的LinqProvider *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤
使用场景: 假如你的项目(这里指的是手机客户端项目)的某个版本(例如1.0版本)已经完成开发.测试并已经上线了,接下来接到新的需求,新需求的开发需要修改多个文件中的代码,当需求已经开始开发一段时间的时 ...
- 浅谈JVM-图解类加载机制
一.目录 二.类加载机制流程 1.什么是类加载机制? JVM把class文件加载到内存里面,并对数据进行校验.准备.解析和初始化,最终能够被形成被JVM可以直接使用的Java类型的过程. 2.类加载流 ...