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传过来的值的方法,总共有两种方式,通过 $ ...
随机推荐
- [转]使用BCP导出导入数据
本文转自:http://www.cnblogs.com/zerocc/p/3225723.html bcp 实用工具可以在 Microsoft SQL Server 实例和用户指定格式的数据文件间大容 ...
- CSS 通过使用Important覆盖所有其他样式
在许多情况下,您将使用CSS库.这些可能会意外覆盖您自己的CSS.所以当你绝对需要确定一个元素具有特定的CSS时,可以使用 !important. 让我们回到之前的 pink-text class 声 ...
- 单点登录在asp.net中的简单实现
系统的基本架构 我们假设一个系统System包含Service客户服务中心.Shop网上购物中心和Office网上办公中心三个独立的网站.Service管理客户的资料,登录和注销过程.不论客户访问Sy ...
- web前端开发教程系列-3 - 前端开发调试工具分享
前言 一. Bug出现的原因 1. 主观原因或者是出现这么多浏览器的原因 2. 另外客观原因: 二. 调试工具 1. Firebug 2. Chrome 3. IE11 4. IETester 5. ...
- PowerDesigner最基础的使用方法入门学习(转载)
来源:http://www.cnblogs.com/biehongli/p/6025954.html 1:入门级使用PowerDesigner软件创建数据库(直接上图怎么创建,其他的概念知识可自行学习 ...
- Java基础教程(10)--类
一.声明类 你已经见过了以如下方式定义的类: class MyClass { // field, constructor, and method declarations } 上面是声明类的最 ...
- 1 springboot创建项目
文章采用idea工具进行springboot项目创建 1点击 New Project 选择[Spring Initializr] 选择Jdk版本其他默认即可 点击Next 2添加项目信息 文章即使用默 ...
- Java 使用new Thread和线程池的区别
本文转至:https://www.cnblogs.com/cnmenglang/p/6273761.html , 孟凡柱的专栏 的博客,在此谢谢博主! 1.new Thread的弊端执行一个异步任务你 ...
- c#无边窗体实现移动的两种方式
转载:http://blog.csdn.net/dxsh126/article/details/2940226 首先,要用到一个WimdowsAPI函数,因此必须引入 using System.Run ...
- BZOJ4475 [Jsoi2015]子集选取
Description 有一些\(\{1\dots n\}\)的子集\(A_{i,j}, 1\leq j\leq i\leq k\)共\(\frac{k(k+1)}2\)个,满足\(A_{i,j}\s ...