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的点击事件的更多相关文章

  1. 关于百度地图InfoWindow响应自定义布局点击事件

    大概讲解: 在百度地图上显示一个marker,当marker被点击后,显示自定义的View.当自定义的View被点击后,响应不同Button的点击事件.被百度这个infowindo里面的view坑惨了 ...

  2. (二)AS给button添加点击事件

    三种方法给Button添加点击事件 (一)通过button的id,添加继承View.OnClickListener的监听实现 <Button android:id="@+id/btn_ ...

  3. Android Listview中Button按钮点击事件冲突解决办法

    今天做项目时,ListView中含有了Button组件,心里一早就知道肯定会有冲突,因为以前就遇到过,并解决过,可惜当时没有记录下来. 今天在做的时候,继续被这个问题郁闷了一把,后来解决后,赶紧来记录 ...

  4. 区分listview的item和Button的点击事件

    这两天修改领导通的ListView widget,在ListView中加入Button这类的有 “点击” 事件的widget,发现原来listview的itemclick居然失效了, 后来在网上查资料 ...

  5. UITapGestureRecognizer会屏蔽掉Button的点击事件( 转载)

    UITapGestureRecognis 前几天在做项目的时候,遇到这个一个问题,在一个视图也就是UIView上添加一个手势,然后又在这个View上添加一个UIButton,然后给按钮添加事件,运行项 ...

  6. NGUI Button 3中点击事件的触发

    NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直接绑定在按钮上,点击按钮触发的方法名必须为OnClick,当 ...

  7. pyqt的多Button的点击事件的槽函数的区分发送signal的按钮。

    关键函数:QPushButton的setObjectName()/objectName() 个人注解:按功能或者区域,将按钮的点击事件绑定的不同的槽函数上. from PyQt5.QtWidgets ...

  8. ListView中响应item的点击事件并且刷新界面

    ---恢复内容开始--- 最近在在实现listview功能中遇到了这个问题: 点击事件写在了adapter的item中,不知道如何在listview的点击事件中更新数据的显示: 总结:1.要使用not ...

  9. Android 实现的EditText响应drawableRight的点击事件

    1.自定义Edittext 实现右侧图标点击清空 package com.dxw.live.view; import android.content.Context; import android.g ...

随机推荐

  1. ABBYY FineReader 12PDF选项卡有保存模式吗

    PDF是广泛使用的文档格式.在ABBYY Finereader中PDF文档的显示不会因电脑不同而有差异,可加密保护,非常适合在电子存档中进行保存 该选项卡上的保存选项分为以下几类: 默认纸张大小 从下 ...

  2. JSON 序列化和反序列化——JavaScriptSerializer实现

    一. JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.您无法访问序列化程序的此实例.但是,此类公开了公共 API.因此, ...

  3. SQL server 2008 Express Edition实现自动备份和自动删除备份

    1.查看SQL 版本: select @@VERSION --可以看到 Express Edition 精简免费版 Microsoft SQL Server 2008 R2 (SP2) - 10.50 ...

  4. CARP-VRRP-HSRP

    CARP-VRRP-HSRP http://www.openbsd.org/faq/pf/carp.html

  5. unity, ugui input field

    ugui Input Field,获取输入的字符串. 错误方法: string content=inputField.FindChild("Text").text; 这样得到的是输 ...

  6. TKinter的常用组件

    python提供了多个图形开发界面的库,几个常用Python GUI库如下: Tkinter: Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.T ...

  7. html之内联标签a

    a标签:定义超链接,用于从一个页面链接到另一个页面,最重要的属性href,如果没有href属性,则不可以使用如下属性:download,hreflang,media,rel,target,type. ...

  8. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  9. shiro权限验证标签

    实例: spring-shiro.xml /admin/repairType/index = roles["ROLE_ADMIN"] /admin/user=roles[" ...

  10. eclipse使用基础--让toolbar显示自己想要的内容

    windows==>customize perspective