第十篇、自定义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等.代码非原创,也是从网上看到的,用到了实际项目中,目前还没发现什么问题. ...
随机推荐
- 转载 DevOps的基本原则与介绍
转载原地址: http://www.cnblogs.com/wintersun/p/3339047.html DevOps的基本原则与介绍 DevOps这个术语是developer与operatio ...
- oracle的commit
oracle的commit就是提交数据(释放锁),在未提交前你前面的操作更新的都是内存,没有更新到物理文件中.执行commit从用户角度讲就是更新到物理文件了,事实上commit时还没有写date f ...
- 重金悬赏的微软:提交Win8漏洞以及发布Win8应用
随着Windows 8.1这个饱受诟病的操作系统的推出,微软想一举改变颓势,也只有从用户体验上下手了. 近来,微软针对Windows 8.1的漏洞,推出了三项奖励措施: 1.对于发现关键性漏洞,并且这 ...
- 写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn reverseList
package homework004; import java.util.ArrayList; import java.util.List; public class Daoxu { public ...
- Postfix上的反垃圾邮件的四个方法
在介绍如何配置Postfix的smtp配置之前有必要首先介绍一下它的背景和特点.Postfix是一个由IBM资助下由WietseVenema 负责开发的自由软件工程的一个产物,其目的是为用户提供除se ...
- 简单的谈一下.NET下的AOP
AOP有着它优良的好处, 从一个层面上去关心一个问题, .NET对此有良好的支持.如果硬生生的看MSDN不理解里面的思想是有掉入深渊的感觉的. 为了理解AOP的机制, 我们从需求开始说起,实现AOP就 ...
- windows 2003 远程登录时如何修改管理员密码
今天买的vps,需要修改密码.但是自己不会,看网上好多人都说是,按ctrl+alt+del .但是我试过之后发现不对,后来又找到说是使用ctrl+alt+end 更改密码就可以了. 千万不要通过那个 ...
- Nuget控制台 - 给你的快速添加缺少的包
利用命令行安装包
- 新浪旗下的SAE云服务入门
前言: 云服务已经火热很多年了,到了现在已经基本进入稳定期,稍微有实力的公司都会推出免费的云存储,像百度的还几个T,速度也不错.如果不担心数据安全问题,代替平时的U盘还是没有问题.而SAE是新浪在20 ...
- iOS开发——新特性OC篇&Swift 2.0新特性
Swift 2.0新特性 转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心.我同样也是非常喜爱这门新的编程语言. 今年6月,一年一度 ...