用block响应button的点击事件
1.继承UIButton ;
2.在自己定义的button类中的方法
addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents 实现block的触发
代码示例:
// ZJBlockButton.h
// BlockTest
//
// Created by 何助金 on 15/4/5.
// Copyright (c) 2015年 何助金. All rights reserved.
//
#import <UIKit/UIKit.h>
@classZJBlockButton;
typedef void (^ButtonBlock)(ZJBlockButton *);//定义一个block
@interface ZJBlockButton : UIButton
@property (nonatomic,copy)ButtonBlock block;
@end
// ZJBlockButton.m
// BlockTest
//
// Created by 何助金 on 15/4/5.
// Copyright (c) 2015年 何助金. All rights reserved.
//
#import "ZJBlockButton.h"
@implementation ZJBlockButton
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[selfaddTarget:selfaction:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
returnself;
}
- (void)buttonClick:(ZJBlockButton *)sender
{
_block(self);
}
3.使用:
, , , )];
zjButton.block = ^(ZJBlockButton *button){
NSLog(@"button click!");
};
[zjButton setTitle:@"touchButton"forState:UIControlStateNormal];
zjButton.backgroundColor = [UIColor grayColor];
[self.view addSubview:zjButton];
PS:可以用同样的方法实现 alertView的Block
// ZJAlertView.h
// BlockTest
//
// Created by 何助金 on 15/4/5.
// Copyright (c) 2015年 何助金. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void (^AlertBlock)(NSInteger);//定义block类型
@interface ZJAlertView : UIAlertView
@property (nonatomic,copy)AlertBlock block;
//需要自定义初始化方法 添加参数 block:(AlertBlock)block;
-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles block:(AlertBlock)block;
@end
的方法实现 alertView的block响应 直接上代码
// ZJAlertView.m
// BlockTest
//
// Created by 何助金 on 15/4/5.
// Copyright (c) 2015年 何助金. All rights reserved.
//
#import "ZJAlertView.h"
@implementation ZJAlertView
-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles block:(AlertBlock)block
{
self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil];
if (self) {
self.block = block ;//block 绑定
}
returnself;
}
//#pragma mark -AlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//这里调用函数指针_block(要传进来的参数);
_block(buttonIndex);
}
应用:
- (void)creatBlockAlertView
{
ZJAlertView *alertView = [[ZJAlertViewalloc]initWithTitle:@"test"message:@"alert Block "delegate:nilcancelButtonTitle:@"cancel"otherButtonTitles:@"Ok"block:^(NSInteger index) {
NSLog(@"click at index %ld",index);
}];
[alertView show];
}
用block响应button的点击事件的更多相关文章
- 关于百度地图InfoWindow响应自定义布局点击事件
大概讲解: 在百度地图上显示一个marker,当marker被点击后,显示自定义的View.当自定义的View被点击后,响应不同Button的点击事件.被百度这个infowindo里面的view坑惨了 ...
- (二)AS给button添加点击事件
三种方法给Button添加点击事件 (一)通过button的id,添加继承View.OnClickListener的监听实现 <Button android:id="@+id/btn_ ...
- Android Listview中Button按钮点击事件冲突解决办法
今天做项目时,ListView中含有了Button组件,心里一早就知道肯定会有冲突,因为以前就遇到过,并解决过,可惜当时没有记录下来. 今天在做的时候,继续被这个问题郁闷了一把,后来解决后,赶紧来记录 ...
- 区分listview的item和Button的点击事件
这两天修改领导通的ListView widget,在ListView中加入Button这类的有 “点击” 事件的widget,发现原来listview的itemclick居然失效了, 后来在网上查资料 ...
- UITapGestureRecognizer会屏蔽掉Button的点击事件( 转载)
UITapGestureRecognis 前几天在做项目的时候,遇到这个一个问题,在一个视图也就是UIView上添加一个手势,然后又在这个View上添加一个UIButton,然后给按钮添加事件,运行项 ...
- NGUI Button 3中点击事件的触发
NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直接绑定在按钮上,点击按钮触发的方法名必须为OnClick,当 ...
- pyqt的多Button的点击事件的槽函数的区分发送signal的按钮。
关键函数:QPushButton的setObjectName()/objectName() 个人注解:按功能或者区域,将按钮的点击事件绑定的不同的槽函数上. from PyQt5.QtWidgets ...
- ListView中响应item的点击事件并且刷新界面
---恢复内容开始--- 最近在在实现listview功能中遇到了这个问题: 点击事件写在了adapter的item中,不知道如何在listview的点击事件中更新数据的显示: 总结:1.要使用not ...
- Android 实现的EditText响应drawableRight的点击事件
1.自定义Edittext 实现右侧图标点击清空 package com.dxw.live.view; import android.content.Context; import android.g ...
随机推荐
- FreeSWITCH在呼叫失败的情况下播放语音提示
看到好多网友问到这个问题.一般我们在打电话时会听到“您拨的电话正在通话中,请稍后再拨....”,或“电话无应答...”之类的提示,我们在 freeswitch 里也可以这样做. 其实很简单,默认的配置 ...
- MySQL-使用tcpdump排查MySQLl数据库tps飙升的问题
可以直接输出 tcpdump -i eth1 -s -l - | strings | perl -e ' while(<>) { chomp; next if /^[^ ]+[ ]*$/; ...
- LNMP-查看安装编译时参数
查看mysql编译参数: cat /usr/local/mysql/bin/mysqlbug | grep CONFIGURE_LINE 查看apache编译参数: cat $apachehome$/ ...
- lucene文件格式待整理
这是之前Lucene3.0生成的索引格式 a表
- 输入n行整数,每行的个数不确定,整数之间用逗号分隔
/*===================================== 输入n行整数,每行的个数不确定. 每行内部两个数之间用逗号隔开. 例如输入数据如下: 6 1,3,5,23,6,8,14 ...
- 【转】第5篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:自动注册JS脚本+委托回调方法分析
作者: 牛A与牛C之间 时间: 2013-11-19 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第5篇:Xilium CefGlue 关于 CLR Object 与 JS ...
- 【转】POP3、SMTP和IMAP之间的区别和联系
POP3 POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议.它是因特网电子邮件的第 ...
- collection的框架结构
经常会看到程序中使用了记录集,常用的有Collection.HashMap.HashSet.ArrayList,因为分不清楚它们之间的关系,所以在使用时经常会混淆,以至于不知道从何下手.在这儿作了一个 ...
- Object-c-数组的使用
一.数组: 1.数组初始化: a.NSArray *array = [[NSArray alloc] init]; b.NSArray *array = [[NSArray array]; 2.初始化 ...
- 【linux】man和--help
man和--help都是获取命令帮助的指令. man 格式: man [命令] [root@andon tmp]# man pwd --help 命令 --help [root@andon tmp]# ...