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:^{

}];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 2,392评论 0 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 1,828评论 0 1
  • 前言 1 MagicNumber->autoresizingMask->autolayout 以上是纯手写代码所经...
    Daimer阅读 479评论 0 2
  • iOS_autoLayout_Masonry 概述 Masonry是一个轻量级的布局框架与更好的包装AutoLay...
    指尖的跳动阅读 1,195评论 1 4
  • 我曾经玩笑 小鹿乱撞 大概不适合我 但后来 你跃动在我周身的空气里 我的心是欢喜的 那只小鹿 也在跟随你跃动 再后...
    Kariii阅读 143评论 1 1