一個(gè)vc需要加一個(gè)view,一個(gè)view上面有imageView,label,button等。我們把這個(gè)view獨(dú)立一個(gè)類出來,button加點(diǎn)擊事件。定義一個(gè)簡(jiǎn)單的block,作為點(diǎn)擊事件的一個(gè)參數(shù),在vc中重新穿件這個(gè)view的實(shí)例,add進(jìn)去,然后setblock,就可以直接回調(diào),響應(yīng)定義好的點(diǎn)擊事件。
類TYDDownLoadTianyaQIngView
.h文件
#import?
@interface?TYDDownLoadTianyaQingView :?UIView
// 定義一個(gè)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?=?@"熱門版塊、精彩互動(dòng)\n盡在天涯社區(qū)官方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"]];
// 加點(diǎn)擊事件
[_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,就可以在其他頁面?zhèn)魅隷block的值,實(shí)現(xiàn)點(diǎn)擊事件回調(diào)
_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中實(shí)例化TYDDownLoadTianyaQingView
_downLoadView?= [[TYDDownLoadTianyaQingView?alloc]?initBasicViewWithFrame:CGRectMake(10,?self.view.frame.size.height-140,?self.view.frame.size.width-20, 60)];
調(diào)用block的set方法實(shí)現(xiàn)回調(diào)
[_downLoadView?setClickBlock:^{
}];