UIButton的两种block传值方式

简介:

UIButton的两种block传值方式

方式1 - 作为属性来传值

BlockView.h 与 BlockView.m

//
//  BlockView.h
//  Block
//
//  Created by YouXianMing on 15/1/14.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>
@class BlockView;

/**
 定义枚举值
 */
typedef enum : NSUInteger {
    LEFT_BUTTON = 0x19871220,
    RIGHT_BUTTON,
} BUTTON_FLAG;

/**
 *  定义block
 *
 *  @param flag      枚举值
 *  @param blockView 当前的blockView
 */
typedef void (^ButtonEvent)(BUTTON_FLAG flag, BlockView *blockView);

@interface BlockView : UIView

@property (nonatomic, copy)   ButtonEvent   buttonEvent; // 作为属性的block

@property (nonatomic, strong) NSString     *leftTitle;
@property (nonatomic, strong) NSString     *rightTitle;

@end


//
//  BlockView.m
//  Block
//
//  Created by YouXianMing on 15/1/14.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "BlockView.h"

@interface BlockView ()

@property (nonatomic, strong) UIButton  *leftButton;
@property (nonatomic, strong) UIButton  *rightButton;

@end

@implementation BlockView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        // 获取尺寸相关内容
        CGFloat width       = frame.size.width;
        CGFloat height      = frame.size.height;
        CGFloat buttonWidth = width / 2.f;
        
        // 初始化按钮
        self.leftButton     = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, height)];
        self.leftButton.tag = LEFT_BUTTON;
        [self.leftButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.leftButton setTitleColor:[UIColor redColor]  forState:UIControlStateHighlighted];
        [self.leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
        [self.leftButton addTarget:self
                            action:@selector(buttonEvents:)
                  forControlEvents:UIControlEventTouchUpInside];
        self.leftButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:12.f];
        [self addSubview:self.leftButton];
        
        self.rightButton     = [[UIButton alloc] initWithFrame:CGRectMake(buttonWidth, 0, buttonWidth, height)];
        self.rightButton.tag = RIGHT_BUTTON;
        [self.rightButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.rightButton setTitleColor:[UIColor redColor]  forState:UIControlStateHighlighted];
        [self.rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
        [self.rightButton addTarget:self
                             action:@selector(buttonEvents:)
                   forControlEvents:UIControlEventTouchUpInside];
        self.rightButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:12.f];
        [self addSubview:self.rightButton];
        
    }
    return self;
}

- (void)buttonEvents:(UIButton *)button {
    // 如果有block值,则从block获取值
    if (self.buttonEvent) {
        self.buttonEvent(button.tag, self);
    }
}

#pragma mark - 重写setter,getter方法
@synthesize leftTitle = _leftTitle;
- (void)setLeftTitle:(NSString *)leftTitle {
    _leftTitle = leftTitle;
    [self.leftButton setTitle:leftTitle forState:UIControlStateNormal];
}
- (NSString *)leftTitle {
    return _leftTitle;
}

@synthesize rightTitle = _rightTitle;
- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    [self.rightButton setTitle:rightTitle forState:UIControlStateNormal];
}
- (NSString *)rightTitle {
    return _rightTitle;
}

@end

控制器源码:
//
//  ViewController.m
//  Block
//
//  Created by YouXianMing on 15/1/14.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "BlockView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    BlockView *blockView        = [[BlockView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    blockView.layer.borderWidth = 1.f;
    blockView.leftTitle         = @"YouXianMing";
    blockView.rightTitle        = @"NoZuoNoDie";
    
    // 从block中获取到事件
    blockView.buttonEvent       = ^(BUTTON_FLAG flag, BlockView *blockView) {
        NSLog(@"%lu", flag);
    };
    
    blockView.center            = self.view.center;
    
    [self.view addSubview:blockView];
}

@end

 

方式2 - 作为方法来传值

BlockView.h 与 BlockView.m

//
//  BlockView.h
//  Block
//
//  Created by YouXianMing on 15/1/14.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>
@class BlockView;

/**
 定义枚举值
 */
typedef enum : NSUInteger {
    LEFT_BUTTON = 0x19871220,
    RIGHT_BUTTON,
} BUTTON_FLAG;

/**
 *  定义block
 *
 *  @param flag      枚举值
 *  @param blockView 当前的blockView
 */
typedef void (^ButtonEvent)(BUTTON_FLAG flag, BlockView *blockView);

@interface BlockView : UIView

@property (nonatomic, strong) NSString     *leftTitle;
@property (nonatomic, strong) NSString     *rightTitle;

/**
 *  定义成方法来实现
 *
 *  @param buttonEvent block
 */
- (void)buttonEvent:(ButtonEvent)buttonEvent;

@end


//
//  BlockView.m
//  Block
//
//  Created by YouXianMing on 15/1/14.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "BlockView.h"

@interface BlockView ()

@property (nonatomic, strong) UIButton    *leftButton;
@property (nonatomic, strong) UIButton    *rightButton;
@property (nonatomic, copy)   ButtonEvent  buttonEvent;

@end

@implementation BlockView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        // 获取尺寸相关内容
        CGFloat width       = frame.size.width;
        CGFloat height      = frame.size.height;
        CGFloat buttonWidth = width / 2.f;
        
        // 初始化按钮
        self.leftButton     = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, height)];
        self.leftButton.tag = LEFT_BUTTON;
        [self.leftButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.leftButton setTitleColor:[UIColor redColor]  forState:UIControlStateHighlighted];
        [self.leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
        [self.leftButton addTarget:self
                            action:@selector(buttonEvents:)
                  forControlEvents:UIControlEventTouchUpInside];
        self.leftButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:12.f];
        [self addSubview:self.leftButton];
        
        self.rightButton     = [[UIButton alloc] initWithFrame:CGRectMake(buttonWidth, 0, buttonWidth, height)];
        self.rightButton.tag = RIGHT_BUTTON;
        [self.rightButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.rightButton setTitleColor:[UIColor redColor]  forState:UIControlStateHighlighted];
        [self.rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
        [self.rightButton addTarget:self
                             action:@selector(buttonEvents:)
                   forControlEvents:UIControlEventTouchUpInside];
        self.rightButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:12.f];
        [self addSubview:self.rightButton];
    }
    return self;
}

- (void)buttonEvents:(UIButton *)button {
    if (self.buttonEvent) {
        self.buttonEvent(button.tag, self);
    }
}

- (void)buttonEvent:(ButtonEvent)buttonEvent {
    // 初始化block
    self.buttonEvent = ^(BUTTON_FLAG flag, BlockView *blockView) {
        if (buttonEvent) {
            buttonEvent(flag, blockView);
        }
    };
}

#pragma mark - 重写setter,getter方法
@synthesize leftTitle = _leftTitle;
- (void)setLeftTitle:(NSString *)leftTitle {
    _leftTitle = leftTitle;
    [self.leftButton setTitle:leftTitle forState:UIControlStateNormal];
}
- (NSString *)leftTitle {
    return _leftTitle;
}

@synthesize rightTitle = _rightTitle;
- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    [self.rightButton setTitle:rightTitle forState:UIControlStateNormal];
}
- (NSString *)rightTitle {
    return _rightTitle;
}

@end

控制器源码:
//
//  ViewController.m
//  Block
//
//  Created by YouXianMing on 15/1/14.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "BlockView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    BlockView *blockView        = [[BlockView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    blockView.layer.borderWidth = 1.f;
    blockView.leftTitle         = @"YouXianMing";
    blockView.rightTitle        = @"NoZuoNoDie";
    
    [blockView buttonEvent:^(BUTTON_FLAG flag, BlockView *blockView) {
        NSLog(@"%lu", flag);
    }];
    
    blockView.center            = self.view.center;
    
    [self.view addSubview:blockView];
}

@end

目录
相关文章
|
11天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1230 5
|
10天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1214 87
|
10天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1796 13
|
20天前
|
人工智能 运维 安全
|
3天前
|
资源调度
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
234 127
|
4天前
|
前端开发
Promise的then方法返回的新Promise对象有什么特点?
Promise的then方法返回的新Promise对象有什么特点?
177 2