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. Unity3d 适配机型

    1,为了是更多机型能够安装你的游戏,Unity3d Device Filter设置:ARMv6 with VFP: 2,华为C8600,一运行强制停止: 参考网址:http://forum.unity ...

  2. 004. 线程间操作无效: 从不是创建控件“textBox1”的线程访问它

    最简单的方法(不推荐): 在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false; 为什么不推荐上面的方法: 为避免空间造成死锁, .net f ...

  3. 【转】ASP.NET的OnClientClick与OnClick事件【解决了“识别用户在对话框里面选yes或no的问题”】

    OnClientClick是客户端事件方法.一般采用JavaScript来进行处理.也就是直接在IE端运行.一点击就运行. OnClick事件是服务器端事件处理方法,在服务器端,也就是IIS中运行.点 ...

  4. spring基于注解的配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. python3_mechanicalsoup

    python3_mechanicalsoup # !/usr/bin/python3.4 # -*- coding: utf-8 -*- import mechanicalsoup # 事实证明,这个 ...

  6. String 深浅拷贝的测试---有待继续测试

    public class TestString { void test1() { // TODO Auto-generated method stub String str = new String( ...

  7. linux vi 工具的使用

    linux vi 工具的使用 1.打开文件 vi filename 如果存在该文件,则打开,不存在,则删除 2. 进入vi后,按i建,可以输入内容, 按ESC 在按: ,输入wq保存并退出. 3.移动 ...

  8. 【extjs】 extjs5 Ext.grid.Panel 搜索示例

    先看效果图: 页面js: <script type="text/javascript"> /** * 日志类型 store * */ var logTypeStore ...

  9. oc-类目、延展、协议

    -----------------------------------------------Category-------------------------------------- 类目 是在原 ...

  10. 源码安装extundelete以及对遇到问题的解决

    软件下载:http://sourceforge.net/projects/extundelete/ 1.在安装extundelete包./configure时遇到configure: error: C ...