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];

}

最新文章

  1. 实时控制软件设计 第二次作业 myRobot
  2. eclipse中一些设置&amp;配置项
  3. PHP 开发 APP 接口 学习笔记与总结 - XML 方式封装通信接口
  4. iOS - Alamofire 网络请求
  5. bootStrap-2
  6. 机器学习在 IT 运维管理中的必要性!
  7. 如何更改c#项目的App.config文件
  8. Anniversary party(POJ 2342 树形DP)
  9. Hiddenfield控件
  10. java异常基础知识点
  11. mysql alter总结
  12. salesforce apex class call exteral webservice
  13. lua 文件编译相关工具
  14. 微信小程序之wx.showmodal
  15. 初识SqlLite ---.net连接数据库
  16. suoi37 清点更多船只 (卡空间线段树)
  17. iOS 第三方框架-MJExtension
  18. cocos2d-x getParent() 获得一个父类的一个node型指针,转换为父类类型
  19. RSA读取密钥——使用openssl编程
  20. JavaScript Image对象 / Tabel对象 / Select对象 / Form对象

热门文章

  1. ADC 分辨率和精度的区别
  2. Unity脚本系统
  3. [C] zintrin.h : 智能引入intrinsic函数。支持VC、GCC,兼容Windows、Linux、Mac OS X
  4. Android 中使用自定义字体的方法
  5. 20 款超棒免费的 Bootstrap 管理和前端模板
  6. LINQ(LINQ to DataSet)
  7. easyUI之layout
  8. c编码-2
  9. windows下android开发环境搭建
  10. xshell 5连接linux服务器的技巧