以下圖所示:自定義PersonalCenterView,如何在controller拿到按鈕(小箭頭)的點擊方法?bash
- (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;
}
複製代碼
//指定初始化方法
- (instancetype)initWithFrame:(CGRect)frame target:(id)target sel:(SEL)action;
複製代碼
- (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;
}
複製代碼
@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(@"箭頭被點擊了~");
}
複製代碼
#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
複製代碼
//pushButton點擊事件
- (void)clickAction:(UIButton*)button
{
if ([self.delegate respondsToSelector:@selector(clickEnter)]) {
[self.delegate clickEnter];
}
}
複製代碼
@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(@"箭頭被點擊了~");
}
複製代碼
//指定初始化方法
- (instancetype)initWithFrame:(CGRect)frame clickBlock:(void(^)(void))clickBlock;
複製代碼
- (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();
}
}
複製代碼
PersonalCenterView *centerView = [[PersonalCenterView alloc]initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 100) clickBlock:^{
//處理點擊事件
NSLog(@"箭頭被點擊了~");
}];
複製代碼
- (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;
}
複製代碼
//採用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(@"箭頭被點擊了~");
}];
複製代碼
@property (nonatomic,strong) RACSubject* clickSubject;
複製代碼
- (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;
}
複製代碼
PersonalCenterView *centerView = [[PersonalCenterView alloc]initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 100)];
[centerView.clickSubject subscribeNext:^(UIButton *button) {
//處理點擊事件
NSLog(@"箭頭被點擊了~");
}];
複製代碼