用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 ...
随机推荐
- dom4j测试
book.xml <?xml version="1.0" encoding="UTF-8"?><books><book>&l ...
- C++面向对象要点
先说说面向对象思想的一个总体认识 对象通常会有行为,这些行为是靠信息支撑,这些信息包括外部信息和内部信息,对象行为会维护其中的一部分信息 因此对象可以看成是这样一种实体,它获取信息,然后决定自己的行为 ...
- 了解oracle数据库的情况
1.了解你的数据库版本号 2.是否配置了DataGuard? SQL> select protection_mode, protection_level, remote_archive,data ...
- ogg 、 Shareplex和DSG RealSync 对比
主流数据库容灾(复制)工具对比 Oracle Golden Gate Quest Shareplex DSG RealSync 公司概要 公司介绍 GoldenGate公司成立于200 ...
- PCA和Softmax分类比较—Mnist与人脸数据集
PCA人脸识别中三种方法得到的正确率可达到100% 作为对比,单独使用Softmax回归对人脸40*10*92*112的数据分类正确率为97%. 用PCA对MNIST手写数字10*500*28*28识 ...
- unity, sceneview 中拾取球体gizmos
http://answers.unity3d.com/questions/745560/handle-for-clickable-scene-objects.html http://www.jians ...
- RabbitMQ 集群与高可用配置
集群概述 通过 Erlang 的分布式特性(通过 magic cookie 认证节点)进行 RabbitMQ 集群,各 RabbitMQ 服务为对等节点,即每个节点都提供服务给客户端连接,进行消息发送 ...
- Nancy 简单学习
1. 环境 vs2010 , nancy 2. 需要测试的功能 1. 输出 页面输出简单文本 2. 输出json 数据 3. 操作 1. 创建asp.net 空项目 2. 添加引用Nancy库 使用n ...
- mybatis——动态sql
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
- LVM---动态调整磁盘容量
LVM:logical volume manager(逻辑卷管理):LVM屏蔽了底层磁盘布局,方便于动态调整磁盘容量. 一.创建逻辑卷的步骤: 1)通过fdisk 工具将磁盘转换为linux分区 2) ...