用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.使用:

 

 

   ZJBlockButton *zjButton = [[ZJBlockButton alloc]initWithFrame:CGRectMake(100, 300, 90, 90)];

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

 

}

 

转载于:https://www.cnblogs.com/zhujin/p/4394314.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值