现在的位置: 首页 > 综合 > 正文

block回调UIButton

2016年05月28日 ⁄ 综合 ⁄ 共 940字 ⁄ 字号 评论关闭

//  BlockBtn.h

#import <UIKit/UIKit.h>

@interface BlockBtn :
UIButton

@property (nonatomic,copy)
void(^block)(void);

- (id)initWithFrame:(CGRect)frame;

- (void)clickAction;

@end

//  BlockBtn.m

#import "BlockBtn.h"

@implementation BlockBtn

- (id)initWithFrame:(CGRect)frame{

   
self = [super
initWithFrame:frame];

   
if (self) {

        [self
addTarget:self
action:@selector(clickAction)
forControlEvents:UIControlEventTouchUpInside];

    }

    return
self;

}

- (void)clickAction{

   
_block();

}

@end

//  ViewController.m

#import "ViewController.h"

#import "BlockBtn.h"

@interface
ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super
viewDidLoad];

   
BlockBtn * bbtn = [[BlockBtn
alloc] initWithFrame:CGRectMake((375-60)/2,
120, 60,
40
)];

    bbtn.backgroundColor = [UIColor
greenColor];

    bbtn.block = ^(void){

        NSLog(@"button event");

    };

    [self.view
addSubview:bbtn];

    [bbtn
release];

}

- (void)didReceiveMemoryWarning {

    [super
didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

抱歉!评论已关闭.