block简单使用

一个vc需要加一个view,一个view上面有imageView,label,button等。我们把这个view独立一个类出来,button加点击事件。定义一个简单的block,作为点击事件的一个参数,在vc中重新穿件这个view的实例,add进去,然后setblock,就可以直接回调,响应定义好的点击事件。

类TYDDownLoadTianyaQIngView

.h文件

#import 

@interface TYDDownLoadTianyaQingView : UIView

// 定义一个block,返回值void,名字为clickBlock

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

- (id)initBasicViewWithFrame:(CGRect)frame;

- (void)downLoadButtonClick:(void(^)())clickBlock;

@end

.m文件

#import "TYDDownLoadTianyaQingView.h"

@interface TYDDownLoadTianyaQingView ()

@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) UILabel  *describeLabel;

@property (nonatomic, strong) UIButton *downLoadButton;

@end

@implementation TYDDownLoadTianyaQingView

- (id)initBasicViewWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self)

{

self.backgroundColor = [UIColor colorInSkinWithKey:@"useColor2"];

[self initBasicView];

}

return self;

}

- (void)initBasicView

{

_iconImageView  = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"more_gowelun"]];

[self addSubview:_iconImageView];

_describeLabel = [[UILabel alloc] init];

_describeLabel.text = @"热门版块、精彩互动\n尽在天涯社区官方APP";

_describeLabel.font = kUseFont5;

_describeLabel.textColor = [UIColor colorInSkinWithKey:@"useColor5"];

_describeLabel.textAlignment = NSTextAlignmentLeft;

_describeLabel.numberOfLines = 2;

[self addSubview:_describeLabel];

_downLoadButton = [UIButton buttonWithType:UIButtonTypeCustom];

[_downLoadButton setTitle:@"下载" forState:UIControlStateNormal];

[_downLoadButton setTitleColor:[UIColor colorInSkinWithKey:@"subTextColor"] forState:UIControlStateNormal];

_downLoadButton.titleLabel.font = kUseFont5;

[_downLoadButton setBackgroundColor:[UIColor colorInSkinWithKey:@"navTitleTextColor"]];

// 加点击事件

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

[self addSubview:_downLoadButton];

[_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.mas_left).offset(10);

make.size.mas_equalTo(CGSizeMake(29, 29));

make.centerY.equalTo(self.mas_centerY);

}];

[_describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(_iconImageView.mas_right).offset(10);

make.top.equalTo(_iconImageView.mas_top).offset(-2);

make.right.equalTo(_downLoadButton.mas_left).offset(-10);

}];

[_downLoadButton mas_makeConstraints:^(MASConstraintMaker *make) {

make.size.mas_equalTo(CGSizeMake(60, 20));

make.centerY.equalTo(self.mas_centerY);

make.right.equalTo(self.mas_right).offset(-10);

}];

}

- (void)downLoadButtonClick:(void(^)())clickBlock

{

// 使定义的block等于传入的block,就可以在其他页面传入_block的值,实现点击事件回调

_clickBlock = clickBlock;

NSURL * url = [NSURL URLWithString:@"tianyaQing://tianya"];

BOOL installed = [[UIApplication sharedApplication] canOpenURL:url];

if (installed) {

[[UIApplication sharedApplication] openURL:url];

}

else

{

NSString*str = [NSString stringWithFormat:@"http://itunes.apple.com/us/app/id%d",529696004];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

}

}

@end

VC中实例化TYDDownLoadTianyaQingView

_downLoadView = [[TYDDownLoadTianyaQingView alloc] initBasicViewWithFrame:CGRectMake(10, self.view.frame.size.height-140, self.view.frame.size.width-20, 60)];

调用block的set方法实现回调

[_downLoadView setClickBlock:^{

}];

你可能感兴趣的:(block简单使用)