// 自定义导航栏左边按钮
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回调的更多相关文章

  1. 玩转SSRS第十篇---自定义代码

    提到SSRS 那么就不得不提一下自定义代码的功能,通过自定义代码,有时候可以解决一些比较复杂的问题,比如将让指定的数据行应用指定的属性值.此篇将演示如何通过简单结构的自定义代码进行报表样式的基本设计. ...

  2. 我的MYSQL学习心得(十) 自定义存储过程和函数

    我的MYSQL学习心得(十) 自定义存储过程和函数 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心 ...

  3. 自定义UIBarButtonItem

    如果是通过UIButton自定义UIBarButtonItem,那么通过如下这个方式设置title是无效的.必须要直接给button设置title. self.navigationItem.right ...

  4. 第十篇 Integration Services:高级事件行为

    本篇文章是Integration Services系列的第十篇,详细内容请参考原文. 简介在前一篇, we introduced fault tolerance by examining method ...

  5. 第十篇 SQL Server安全行级安全

    本篇文章是SQL Server安全系列的第十篇,详细内容请参考原文. 不像一些其他industrial-strength数据库服务,SQL Server缺乏一个内置保护个别数据记录的机制,称为行级安全 ...

  6. Python开发【第二十篇】:缓存

    Python开发[第二十篇]:缓存redis&Memcache   点击这里 Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy ...

  7. 【译】第十篇 SQL Server安全行级安全

    本篇文章是SQL Server安全系列的第十篇,详细内容请参考原文. 不像一些其他industrial-strength数据库服务,SQL Server缺乏一个内置保护个别数据记录的机制,称为行级安全 ...

  8. 【译】第十篇 Integration Services:高级事件行为

    本篇文章是Integration Services系列的第十篇,详细内容请参考原文. 简介在前一篇, we introduced fault tolerance by examining method ...

  9. UIButton+Block

    UIButton的一个Category,使用block处理UIControlEvent事件,如常用的TouchUpInside等.代码非原创,也是从网上看到的,用到了实际项目中,目前还没发现什么问题. ...

随机推荐

  1. hdoj 4548 美素数

    美素数 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...

  2. define定义方法

    define('a',function(){ ; return x; }); define('b',['a'],function(a){ ; return y+a; }); M.use(['b'],f ...

  3. 一、FreeMarker 模版开发指南 第一章 入门

    所有资料来自 南磊 翻译的官方文档,我弄简单了,适合自己以后拿出来翻看. 章节内容如下: 简介 模板+数据模型=输出 数据模型一览 模板一览 一.模板  +  数据模型  =  输出 输出结果: &l ...

  4. C# 条形码 生成函数 (Code 128 标准

    C# 条形码 生成函数 (Code 128 标准参考:GB/T 18347-2001) 最近在做单据打印,发现客户要求用到条形码,在网上找了,发现只有一些条形码的标准,但打出来发现根本不能扫,还要加某 ...

  5. 期望-pku-oj-1055:Tree

    题目链接: http://poj.openjudge.cn/practice/1055/ 题目意思: 给出的树最大节点个数为n的情况下,求树上点深度的期望. 解题思路: 数学期望公式的推导. 自己先画 ...

  6. 函数组:FACS(FI/CO接口的FI服务)

    这个函数组可以执行与财务相关的各种检查,具体功能请自行发掘. 包含下列函数: ACC_ROUNDING_DIFF_DETERMINEACC_ROUNDING_DIFF_LINEITEMAC_KURSF ...

  7. windows平台下php版本问题–VC6/VC9和TS/NTS

    php下载页面中提供了4个下载版本,是vc6/vc9 与 TS/NTS的组合 VC6:legacy Visual Studio 6 compiler,就是使用这个编译器编译的.        VC9: ...

  8. android125 zhihuibeijing 缓存

    ## 三级缓存 ## - 内存缓存, 优先加载, 速度最快 - 本地缓存(内存卡), 次优先加载, 速度快 - 网络缓存, 不优先加载, 速度慢,浪费流量 package com.itheima.zh ...

  9. C#_datatable 写入大量数据_BulkCopy

    using Microsoft.Win32; using System; using System.Collections.Generic; using System.Configuration; u ...

  10. 【转】使用BBB的device tree和cape(重新整理版)

    只要你想用BBB做哪怕一丁点涉及到硬件的东西,你就不可避免地要用到cape和device tree的知识.所以尽管它们看起来很陌生而且有点复杂,但还是得学.其实用起来不难的.下面我只讲使用时必须会的内 ...