用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 ...
随机推荐
- 初识boost之boost::share_ptr用法
boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...
- php特殊语法--模板引擎中比较常见
<?php $a=array(1,2,0); foreach ($a as $v): if($v>1): ?> 5 <?php endif; endforeach; ?> ...
- 自然语言处理2.1——NLTK文本语料库
1.获取文本语料库 NLTK库中包含了大量的语料库,下面一一介绍几个: (1)古腾堡语料库:NLTK包含古腾堡项目电子文本档案的一小部分文本.该项目目前大约有36000本免费的电子图书. >&g ...
- Python几个标准类型内建函数
Python提供了一些内建函数用于基本对象类型:cmp(),repr(),str(),type()和等同于repr()的(' ')操作符 (1)type() type的用法如下: type(objec ...
- requests
>>>import requests>>> r = requests.get('http://www.zhidaow.com') # 发送请求>>&g ...
- OpenJudge计算概论-四大湖
/*====================================================================== 四大湖 总时间限制: 1000ms 内存限制: 655 ...
- CA接口测试类
package com.creditharmony.adapter.testCase.ca; import org.junit.Test; import com.alibaba.druid.util. ...
- Java compiler level does not match the version of the installed Java project facet.问题
从同事那里拷贝过来的web项目,导入到eclipse中,出现Java compiler level does not match the version of the installed Java p ...
- bootstrapDialog插件集成datatables插件遇到的异常
最近项目中,涉及到很多细分领域的东西,有好些目前还没有详细的方案.这是后话,当前起步阶段,我要把握技术路线,搭建基础架构!其中,有好几个地方都用到模态框(Modal), 虽然Bootstrap框架里面 ...
- jquery radio的取值 radio的选中 radio的重置
radio 按钮组, name=”sex”. <input type="radio" name="sex" value="Male"& ...