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

栏目: IOS · 发布时间: 5年前

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

controller如何拿到自定义view的点击事件?
复制代码

UI布局代码如下所示

- (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 = @"陈小丸:dango:";
    }
    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文件

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

2).m文件

- (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文件

@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文件

#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文件

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

3)controller文件

@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文件

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

2).m文件

- (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文件

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

复制代码

4.kvo

1).m文件

- (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文件

//采用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文件

@property (nonatomic,strong)  RACSubject* clickSubject;
复制代码

2).m文件

- (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文件

PersonalCenterView *centerView = [[PersonalCenterView alloc]initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 100)];
    
[centerView.clickSubject subscribeNext:^(UIButton *button) {
    //处理点击事件
    NSLog(@"箭头被点击了~");
}];

复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

国际游戏设计全教程

国际游戏设计全教程

[美]迈克尔·萨蒙德 / 张然、赵嫣 / 中国青年出版社 / 2017-2 / 108.00元

你想成为一名电子游戏设计师吗?想知道《肯塔基0号路》《到家》《枪口》等独立游戏的制作理念及过程吗?想了解《戈莫布偶大冒险》《辐射3》《战争机器》中关卡设计的奥秘吗?本书用通俗易懂的文字介绍了在游戏开发与策划过程中,需要掌握的游戏设计原理和制作的基础知识,可以作为读者从“构思一个电子游戏”到“真正完成一个电子游戏”的完备指南。 本书以系统的游戏设计流程结合大量优秀的游戏设计案例进行讲解,让读者......一起来看看 《国际游戏设计全教程》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试