// 自定义导航栏左边按钮
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. nyoj 811 变态最大值

    变态最大值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解 ...

  2. ASP.NET基础系列

    一.HttpContext概述 1).如何获取对象: 在WebForm或类库(包括MVC)项目中,通过Current静态属性,就能够获得HttpContext的对象: HttpContext cont ...

  3. BootStrap最常用的几个插件(V3.3.0版)

    1.标签页 <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> < ...

  4. (二)Bootstrap CSS 概览

    在这一章中,我们将讲解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML 5 文档类型(Doctype) Bootstrap 使用了一些 H ...

  5. NGUI学习笔记(一):官方视频学习记录

    学习NGUI一直断断续续的,目前打算做一个总结的笔记. 我使用的是比较老的3.6.0版本. 1.使用NGUI,需要开启“Edit”->“Project Settings”->“Physic ...

  6. 【UNIX】select、poll、epoll学习

    三者都是UNIX下多路复用的内核接口,select是跨平台的接口,poll是systemV标准,epoll是linux专有的接口,基于poll改造而成. select 函数原型: int select ...

  7. MySQL的安装——源码方式(实验环境下测试用,真实环境请忽略此文)

    #虚拟机是最初的的系统,我们在虚拟机里安装scp [root@serv01 ~]# yum install /usr/bin/scp -y #安装过程略 #我们拷贝MySQL的源码包到目标机的/roo ...

  8. JQuery.Ajax之错误调试帮助信息介绍

    下面是Jquery中AJAX参数详细列表: timeout Number 设置请求超时时间(毫秒).此设置将覆盖全局设置. async Boolean (默认: true) 默认设置下,所有请求均为异 ...

  9. Project Management - 1) Schedule Your Project

    1. 根据项目的理解,列出主要的里程碑. (初始甘特图) 2. 多使用即时贴,甘特图可以贴在墙上以供项目组成员提醒. 3. 切忌过早细化项目日程,这样会让老板或出资人以为项目中几乎没有风险, 他们会把 ...

  10. node.js在windows下的学习笔记(5)---用NODE.JS创建服务器和客户端

    //引入http模块 var http = require('http'); //调用http的createServer的方法,这个方法有一个回调函数,这个回调数 //的作用是当有请求发送给服务器的时 ...