第十篇、自定义UIBarButtonItem和UIButton block回调
// 自定义导航栏左边按钮
self.navigationItem.leftBarButtonItem = [JQBlockedBarButtonItem blockedBarButtonItemWithTitle:@"返回" eventHandler:^{
[weakSelf.navigationController popViewControllerAnimated:YES];
}];
// 声明文件提供常用的接口
@interface JQBlockedBarButtonItem : UIBarButtonItem
+ (instancetype)blockedBarButtonItemWithTitle:(NSString *)title eventHandler:(void(^)())eventHandler;
+ (instancetype)blockedBarButtonItemWithImage:(UIImage *)image eventHandler:(void (^)())eventHandler;
+ (instancetype)blockedBarButtonItemWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem eventHandler:(void (^)())eventHandler;
+ (instancetype)blockedBarButtonItemWithCustomView:(UIView *)customView eventHandler:(void (^)())eventHandler;
@end
#import "JQBlockedBarButtonItem.h" @interface JQBlockedBarButtonItem ()
@property (nonatomic, copy) void(^eventHandler)();
@property (nonatomic, strong) UITapGestureRecognizer *customViewTapGestureRecognizer;
@end @implementation GKBlockedBarButtonItem - (instancetype)initWithTitle:(NSString *)title {
self = [super initWithTitle:title style:UIBarButtonItemStylePlain target:nil action:nil]; [self setup]; return self;
} - (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem {
self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil]; [self setup]; return self;
} - (instancetype)initWithImage:(UIImage *)image {
self = [super initWithImage:image style:UIBarButtonItemStylePlain target:nil action:nil]; [self setup]; return self;
} - (void)setup {
self.target = self;
self.action = @selector(tapped);
} - (instancetype)initWithCustomView:(UIView *)customView eventHandler:(void(^)())eventHandler {
self = [super initWithCustomView:customView]; if (eventHandler) {
self.customViewTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(customViewTapGestureRecognizer:)];
[customView addGestureRecognizer:self.customViewTapGestureRecognizer];
}
self.eventHandler = eventHandler; return self;
} - (void)tapped {
if (self.eventHandler) {
self.eventHandler();
}
} - (void)customViewTapGestureRecognizer:(UITapGestureRecognizer *)gr {
[self tapped];
} + (instancetype)blockedBarButtonItemWithTitle:(NSString *)title eventHandler:(void (^)())eventHandler {
GKBlockedBarButtonItem *tmp = [[GKBlockedBarButtonItem alloc] initWithTitle:title];
tmp.eventHandler = eventHandler; return tmp;
} + (instancetype)blockedBarButtonItemWithImage:(UIImage *)image eventHandler:(void (^)())eventHandler {
GKBlockedBarButtonItem *tmp = [[GKBlockedBarButtonItem alloc] initWithImage:image];
tmp.eventHandler = eventHandler; return tmp;
} + (instancetype)blockedBarButtonItemWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem eventHandler:(void (^)())eventHandler {
GKBlockedBarButtonItem *tmp = [[GKBlockedBarButtonItem alloc] initWithBarButtonSystemItem:systemItem];
tmp.eventHandler = eventHandler; return tmp;
} + (instancetype)blockedBarButtonItemWithCustomView:(UIView *)customView {
return [[self class] blockedBarButtonItemWithCustomView:customView eventHandler:nil];
} + (instancetype)blockedBarButtonItemWithCustomView:(UIView *)customView eventHandler:(void (^)())eventHandler {
GKBlockedBarButtonItem *item = [[GKBlockedBarButtonItem alloc] initWithCustomView:customView eventHandler:eventHandler]; return item;
} - (void)dealloc {
if (self.customView && _customViewTapGestureRecognizer) {
[self.customView removeGestureRecognizer:_customViewTapGestureRecognizer];
}
}
@end
// 自定义按钮,特意选了个最长的~
CGRect rect = CGRectMake(, , , );
__weak typeof(self) weakSelf = self;
JQBlockedButton *blockedBtn = [JQBlockedButton blockedButtonWithTapHandler:^{
NSLog(@"点击了按钮");
[weakSelf.navigationController pushViewController:[TestViewController new] animated:YES];
} frame:rect addToSuperview:self.view]; blockedBtn.backgroundColor = [UIColor redColor];
@interface JQBlockedButton : UIButton
/**
* 点击事件回调
*/
@property (nonatomic, copy) void(^tapHandler)();
/**
* 快速创建带block回调的按钮
*
* @param tapHandler 回调事件
*
*/
+ (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler;
/**
* 快速创建带block回调的按钮
*
* @param tapHandler 回调事件
* @param frame 位置信息
*/
+ (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler frame:(CGRect)frame;
/**
* 快速创建带block回调的按钮,并添加到父控件
*
* @param tapHandler 回调事件
* @param frame 位置信息
* @param superview 父控件
*/
+ (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler frame:(CGRect)frame addToSuperview:(UIView *)superview;
@end
@implementation JQBlockedButton - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame]; [self setup]; return self;
} - (void)awakeFromNib {
[super awakeFromNib]; [self setup];
} - (void)setup {
[self addTarget:self action:@selector(tapped) forControlEvents:UIControlEventTouchUpInside]; } - (void)tapped {
if (_tapHandler) {
_tapHandler();
}
} + (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler {
return [self blockedButtonWithTapHandler:tapHandler frame:CGRectNull addToSuperview:nil];
} + (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler frame:(CGRect)frame {
return [self blockedButtonWithTapHandler:tapHandler frame:frame addToSuperview:nil];
} + (instancetype)blockedButtonWithTapHandler:(void(^)())tapHandler frame:(CGRect)frame addToSuperview:(UIView *)superview {
GKBlockedButton *button = [GKBlockedButton new];
button.tapHandler = tapHandler; if (!CGRectIsNull(frame)) {
button.frame = frame;
} if (superview) {
[superview addSubview:button];
} return button;
} @end
第十篇、自定义UIBarButtonItem和UIButton block回调的更多相关文章
- 玩转SSRS第十篇---自定义代码
提到SSRS 那么就不得不提一下自定义代码的功能,通过自定义代码,有时候可以解决一些比较复杂的问题,比如将让指定的数据行应用指定的属性值.此篇将演示如何通过简单结构的自定义代码进行报表样式的基本设计. ...
- 我的MYSQL学习心得(十) 自定义存储过程和函数
我的MYSQL学习心得(十) 自定义存储过程和函数 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心 ...
- 自定义UIBarButtonItem
如果是通过UIButton自定义UIBarButtonItem,那么通过如下这个方式设置title是无效的.必须要直接给button设置title. self.navigationItem.right ...
- 第十篇 Integration Services:高级事件行为
本篇文章是Integration Services系列的第十篇,详细内容请参考原文. 简介在前一篇, we introduced fault tolerance by examining method ...
- 第十篇 SQL Server安全行级安全
本篇文章是SQL Server安全系列的第十篇,详细内容请参考原文. 不像一些其他industrial-strength数据库服务,SQL Server缺乏一个内置保护个别数据记录的机制,称为行级安全 ...
- Python开发【第二十篇】:缓存
Python开发[第二十篇]:缓存redis&Memcache 点击这里 Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy ...
- 【译】第十篇 SQL Server安全行级安全
本篇文章是SQL Server安全系列的第十篇,详细内容请参考原文. 不像一些其他industrial-strength数据库服务,SQL Server缺乏一个内置保护个别数据记录的机制,称为行级安全 ...
- 【译】第十篇 Integration Services:高级事件行为
本篇文章是Integration Services系列的第十篇,详细内容请参考原文. 简介在前一篇, we introduced fault tolerance by examining method ...
- UIButton+Block
UIButton的一个Category,使用block处理UIControlEvent事件,如常用的TouchUpInside等.代码非原创,也是从网上看到的,用到了实际项目中,目前还没发现什么问题. ...
随机推荐
- iOS CAShapeLayer精讲
前言 CAShapeLayer继承自CALayer,因此,可使用CALayer的所有属性.但是,CAShapeLayer需要和贝塞尔曲线配合使用才有意义. 关于UIBezierPath,请阅读文章:i ...
- 设置 ubuntu ftp
apt-get install后就是启动不了,ftp localhost connection refued 1. 关闭ubuntu防火墙 : ufw disable 2. 还是不行,报 500 m ...
- 代码自动生成工具MyGeneration之一(程序员必备工具)
代码自动生成工具MyGeneration之一(程序员必备工具) 转 分类: C#2008-08-06 18:12 16064人阅读 评论(12) 收藏 举报 工具数据库相关数据库stringbrows ...
- 分析Model2系统心得
分析Model2系统心得 前言:观摩他人的项目,学到一些新的.实践经验呀!!! 1. 怎样使用字符串处理类?从页面获取的Form类或者字段取值时使用. 2.在验证用户身份时,先推断username, ...
- Android 百度地图API 定位 导航
看看这个利用百度地图定位并实现目的地导航的Demo. 首先看实现效果: 进 入后首先会得到当前位置,在地图上显示出来.在输入框中输入目的地后,就会在地 ...
- cocos2d 中加入显示文字的三种方式(CCLabelTTF 、CCLabelBMFont 和CCLabelAtlas)
在 cocos2d 中有三个类能够在层或精灵中加入文字: CCLabelTTF CCLabelBMFont CCLabelAtlas CCLabelTTF CCLabelTTF 每次调用 s ...
- ThinkPHP CURD方法盘点:data方法
data方法也是模型类的连贯操作方法之一,用于设置当前要操作的数据对象的值,可能大家不太习惯用这个方法,今天来讲解下如何用好data方法. 用法 写操作 通常情况下我们都是通过create方法或者赋值 ...
- hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
Conturbatio Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...
- valgrind 生成mysqld调用图之 select now()跟踪
1.mysqld起动方式: 1.mysqld以root用户运行 valgrind --tool=callgrind --separate-threads=yes --trace-children=y ...
- 浅谈iOS中的视图优化
引言: 让我们来思考几个问题,你开发过的产品,它还有可以优化的地方吗?能增加它的帧率吗?能减少多余的CPU计算吗?是不是存在多余的GPU渲染?业务这点工作量对于越来越强大的设备面前显得微不足道,但作为 ...