前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >controller如何拿到自定义view的点击事件?

controller如何拿到自定义view的点击事件?

作者头像
iOSSir
发布2019-06-14 12:15:24
5360
发布2019-06-14 12:15:24
举报

原文作者:小科科

如下图所示:自定义PersonalCenterView,如何在controller拿到按钮(小箭头)的点击方法?

一、UI布局代码如下所示

代码语言:javascript
复制
- (void)initUI
{
    //子视图
    [self addSubview:self.headImageView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.autographLabel];
    [self addSubview:self.pushButton];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImageView.mas_right).mas_offset(15);
        make.top.mas_equalTo(25);
        make.height.mas_equalTo(20);
    }];
    
    [self.autographLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImageView.mas_right).mas_offset(15);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(20);
    }];
    
    [self.pushButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.right.mas_equalTo(-20);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(17);
    }];

}

- (UIImageView*)headImageView
{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc]init];
        _headImageView.backgroundColor = [UIColor redColor];
        _headImageView.layer.masksToBounds = YES;
        _headImageView.layer.cornerRadius = 30;
        _headImageView.image = [UIImage imageNamed:@"head.jpg"];
    }
    return _headImageView;
}

- (UILabel*)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.text = @"陈小丸?";
    }
    return _nameLabel;
}

- (UILabel*)autographLabel
{
    if (!_autographLabel) {
        _autographLabel = [[UILabel alloc]init];
        _autographLabel.font = [UIFont systemFontOfSize:13];
        _autographLabel.numberOfLines = 0;
        _autographLabel.text = @"愿你不知人间疾苦,过得无拘无束";
    }
    return _autographLabel;
}

- (UIButton*)pushButton
{
    if (!_pushButton) {
        _pushButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_pushButton setBackgroundImage:[UIImage imageNamed:@"force_icon"] forState:UIControlStateNormal];
    }
    return _pushButton;
}

二、实现方式

1.SEL

1.h文件

代码语言:javascript
复制
//指定初始化方法
- (instancetype)initWithFrame:(CGRect)frame  target:(id)target  sel:(SEL)action;

2.m文件

代码语言:javascript
复制
- (instancetype)initWithFrame:(CGRect)frame  target:(id)target  sel:(SEL)sel
{
    self = [super initWithFrame:frame];
    if (self) {
        //背景颜色
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        //子视图
        [self initUI];
        [self.pushButton addTarget:target action:sel forControlEvents:(UIControlEventTouchUpInside)];
    }
    return self;
}

3.controller文件

代码语言:javascript
复制
@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    PersonalCenterView *centerView = [[PersonalCenterView alloc]initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 100) target:self  sel:@selector(clickEnter)];
    [self.view addSubview:centerView];
    
}

- (void)clickEnter
{
    //处理点击事件
    NSLog(@"箭头被点击了~");
}

2.代理

1.h文件

代码语言:javascript
复制
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@protocol PersonalCenterViewDelegate <NSObject>

- (void)clickEnter;

@end

@interface PersonalCenterView : UIView

//代理实现
@property (nonatomic, weak) id<PersonalCenterViewDelegate>delegate;

@end

NS_ASSUME_NONNULL_END

2.m文件

代码语言:javascript
复制
//pushButton点击事件
- (void)clickAction:(UIButton*)button
{
    if ([self.delegate respondsToSelector:@selector(clickEnter)]) {
        [self.delegate clickEnter];
    }
}

3.controller文件

代码语言:javascript
复制
@interface ViewController ()<PersonalCenterViewDelegate>


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //代理实现通信
    PersonalCenterView *centerView = [[PersonalCenterView alloc]initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 100)];
    centerView.delegate = self;
    [self.view addSubview:centerView];
    
}

- (void)clickEnter
{
    //处理点击事件
    NSLog(@"箭头被点击了~");
}

3.block

1.h文件

代码语言:javascript
复制
//指定初始化方法
- (instancetype)initWithFrame:(CGRect)frame clickBlock:(void(^)(void))clickBlock;

2.m文件

代码语言:javascript
复制
- (instancetype)initWithFrame:(CGRect)frame clickBlock:(void(^)(void))clickBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        //背景颜色
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        //子视图
        [self initUI];
        
        self.clickBlock = clickBlock;
    }
    return self;
}

- (void)clickAction:(UIButton*)button
{
    if(self.clickBlock)
    {
        self.clickBlock();
    }
}

3.controller文件

代码语言:javascript
复制
PersonalCenterView *centerView = [[PersonalCenterView alloc]initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 100) clickBlock:^{
    //处理点击事件
    NSLog(@"箭头被点击了~");
}];

4.kvo

1.m文件

代码语言:javascript
复制
- (UIButton*)pushButton
{
    if (!_pushButton) {
        _pushButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_pushButton setBackgroundImage:[UIImage imageNamed:@"force_icon"] forState:UIControlStateNormal];
        [_pushButton addTarget:self action:@selector(clickAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _pushButton;
}

- (void)clickAction:(UIButton*)button
{
    //触发kvo
    button.selected = !button.selected;
}

2.controller文件

代码语言:javascript
复制
//采用facebook开源的第三方 备注:这里这是拿来举例说明通信方式 这样的场景不适合
PersonalCenterView *centerView = [[PersonalCenterView alloc]initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 100)];
    
FBKVOController *KVOController = [FBKVOController controllerWithObserver:self];
[KVOController observe:centerView.pushButton keyPath:@"selected" options:(NSKeyValueObservingOptionNew) block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
    NSLog(@"%@",change);
    //处理点击事件 无需判断 因为按钮被点击就会走
    NSLog(@"箭头被点击了~");
}];

5.ReactiveCocoa

1.h文件

代码语言:javascript
复制
@property (nonatomic,strong)  RACSubject* clickSubject;

2.m文件

代码语言:javascript
复制
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.clickSubject = [RACSubject subject];
        //背景颜色
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        //子视图
        [self initUI];
    }
    return self;
}

- (UIButton*)pushButton
{
    if (!_pushButton) {
        _pushButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_pushButton setBackgroundImage:[UIImage imageNamed:@"force_icon"] forState:UIControlStateNormal];
        [[_pushButton  rac_signalForControlEvents:UIControlEventTouchUpInside]  subscribeNext:^(UIButton * button) {
            [self.clickSubject  sendNext:button];
        }];
    }
    return _pushButton;
}

3.controller文件

代码语言:javascript
复制
PersonalCenterView *centerView = [[PersonalCenterView alloc]initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 100)];
    
[centerView.clickSubject subscribeNext:^(UIButton *button) {
    //处理点击事件
    NSLog(@"箭头被点击了~");
}];
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 iOSSir 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档