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传过来的值的方法,总共有两种方式,通过 $ ...
随机推荐
- mongodb3.4.15集群搭建
机器:ubuntu 2台 ip: 192.168.0.131 host1 192.168.0.132 host2 安装mongodb sudo apt-key adv --keyserver hkp: ...
- 业务ID 生成规则
在实际业务中,是否碰到过这种场景: 我们需要一个单号,要在业务系统里面保证唯一,保证增长? 在运营过程,需要知道业务单发生的时间,最好不用经过系统查找就知道发生的时间? 在排障过程中,不用再次查找就知 ...
- 使用DAO模式开发宠物管理系统---hellokitty
宠物有狗和企鹅. 狗的属性有:编号.名称.亲密值.健康值.品种.所属主人编号. 企鹅的属性有:编号.名称.亲密值.健康值.性别.所属主人编号. 该系统中主人可以领养宠物,主人的属性有:编号.用户名.密 ...
- Node.js HTTP Server对象及GET、POST请求
上一博客学习了请求与响应,2次读2次写,但有一个问题就是客户端写入的时候怎么知道请求到达.所以HTTP Server对象出现了.它提供了实现HTTP服务器的基本框架.它可以监听端口的底层套接字和接收请 ...
- 匿名函数、闭包、lambda表达式、Block
C#有lambda.匿名函数,js有匿名函数.闭包,OC中有block,看到这是不是心中有一万个草泥马在跑,不过它们这些都是换汤不换药,不同语言名字不一样. 从功能性上说lambda和closure( ...
- vue常见知识点整理
什么是 mvvm? MVVM 是 Model-View-ViewModel 的缩写.mvvm 是一种设计思想.Model 层代表数据模型,也可以在 Model 中定义数据修改和操作的业务逻辑:View ...
- [javaSE] 位运算符(&|^)
位运算是直接对二进制进行计算 左移 << 右移 >> 先把整数换成四个8bit 0000-0000 0000-0000 0000-0000 0000-0000 这个二进制左右移 ...
- EF6 CodeFirst代码迁移笔记
由于EF7只支持codefirst only.朕无奈被微软逼上了梁山学一下codefirst,就算是为明年做准备吧.写的这些网上大致都有,基本没啥 新内容, 迁移 使用自动迁移 Enable- ...
- 撰写html标签的快捷方式1
一: <ul> <li><a href=""></a></li></ul> 如果要写上面的标签,直接写 ul ...
- 【转】关于JTA,XA,ACID
对于我们这种初学者,可能会使用spring带给我们的@Transactional,可能了解JTA,可能会使用jotm.atomikos,又会遇到一些名词XA,支持XA的数据库驱动等等诸多问题,然后就会 ...