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传值方式的更多相关文章

  1. Windows Azure VM的两种shut down 方式

    今天在调查Azure的价格时,发现下面的语句,来自http://azure.microsoft.com/en-us/pricing/details/virtual-machines/ * If my ...

  2. 两种隐藏元素方式【display: none】和【visibility: hidden】的区别

    此随笔的灵感来源于上周的一个面试,在谈到隐藏元素的时候,面试官突然问我[display: none]和[visibility: hidden]的区别,我当时一愣,这俩有区别吗,好像有,但是忘记了啊,因 ...

  3. Linux共享库两种加载方式简述

      Linux共享库两种加载方式简述  动态库技术通常能减少程序的大小,节省空间,提高效率,具有很高的灵活性,对于升级软件版本也更加容易.与静态库不同,动态库里面的函数不是执行程序本身 的一部分,而是 ...

  4. android环境下两种md5加密方式

    在平时开发过程中,MD5加密是一个比较常用的算法,最常见的使用场景就是在帐号注册时,用户输入的密码经md5加密后,传输至服务器保存起来.虽然md5加密经常用,但是md5的加密原理我还真说不上来,对md ...

  5. Form表单中method=post/get两种数据传输的方式的区别

    Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响.虽然为了方便的得到变量值,Web容器已经屏蔽了二者的一 ...

  6. 两种数据传输的方式——get和post。

    Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响.虽然为了方便的得到变量值,Web容器已经屏蔽了二者的一 ...

  7. Xamarin Android Fragment的两种加载方式

    android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...

  8. nginx 和 tp兼容pathinfo和rewrite两种url访问方式

    环境:centos7,yum安装的nginx1.10.php-fpm,tp3.2 本方法只需要配置nginx.conf的一个文件就可以支持pathinfo和rewrite两种url访问方式 vim / ...

  9. mybatis中两种取值方式?谈谈Spring框架理解?

    1.mybatis中两种取值方式? 回答:Mybatis中取值方式有几种?各自区别是什么? Mybatis取值方式就是说在Mapper文件中获取service传过来的值的方法,总共有两种方式,通过 $ ...

随机推荐

  1. [C语言]类型限定词const解析

    作为C90增加的一个受限类型关键字,const赋予了它修饰的变量一个新属性——不变性,如果一个变量声明中带有关键字const,则无法通过赋值.增减运算来修改该变量的值. 一.指针与const结合 co ...

  2. HihoCoder - 1040 矩形判断

    矩形判断 给出平面上4条线段,判断这4条线段是否恰好围成一个面积大于0的矩形. Input 输入第一行是一个整数T(1<=T<=100),代表测试数据的数量. 每组数据包含4行,每行包含4 ...

  3. awk 统计文件中按照某列统计某列的和(sum)

    把第一列相同的名称的第二列加起来: [root@localhost cc]# cat 1.txtaaa 10 bbb 20aaa 30ccc 40ccc 20ccc 40 [root@localhos ...

  4. The "tsconfig.json" file must have compilerOptions.sourceMap set to true

    在编译ionic项目的时候出现:Error:The "tsconfig.json" file must have compilerOptions.sourceMap set to ...

  5. sql中替换换行符和空格的示例

    select DiscussID,L.Name as LocationName , C.Name as ClientName, REPLACE(BrandName,' ','') BrandName ...

  6. 用PDMReader工具生成数据库设计文档(转载)

    来源:http://blog.csdn.net/xinglun88/article/details/19987719 第一步:下载并安装PDMReader,资源网站: http://www.pdmre ...

  7. Android 蓝牙操作详解

    1.启用蓝牙并使设备处于可发现状态    1.1 在使用BluetoothAdapter类的实例进操作之前,应启用isEnable()方法检查设备是否启用了蓝牙适配器.       // 使用意图提示 ...

  8. 008.在C#中,显式接口VS隐式接口

    原文http://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp (At ...

  9. axios上传图片(及vue上传图片到七牛))

    浏览器上传图片到服务端,我用过两种方法: 1.本地图片转换成base64,然后通过普通的post请求发送到服务端. 操作简单,适合小图,以及如果想兼容低版本的ie没办法用此方法 2.通过form表单提 ...

  10. BZOJ2227 [Zjoi2011]看电影(movie)

    Description \(k\)个座位,\(n\)个人依次过来,每人随机从\(k\)个座位中选择一个,并从它开始不停向后走直到遇到空座位坐下.求所有人都能坐下的概率(即没有人走到第\(k+1\)个位 ...