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