博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIButton的两种block传值方式
阅读量:5034 次
发布时间:2019-06-12

本文共 8609 字,大约阅读时间需要 28 分钟。

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

 

转载于:https://www.cnblogs.com/YouXianMing/p/4225023.html

你可能感兴趣的文章
C#微信登录-手机网站APP应用
查看>>
HTML5实践 -- iPhone Safari Viewport Scaling Bug
查看>>
一位数据挖掘成功人士 给 数据挖掘在读研究生 的建议
查看>>
Python3.6.0安装
查看>>
hdu1049
查看>>
H5项目常见问题及注意事项
查看>>
索尼(SONY) SVE1512S7C 把WIN8降成WIN7图文教程
查看>>
时间模块 && time datetime
查看>>
jquery自动生成二维码
查看>>
spring回滚数据
查看>>
新浪分享API应用的开发
查看>>
美国专利
查看>>
【JavaScript】Write和Writeln的区别
查看>>
百度编辑器图片在线流量返回url改动
查看>>
我对你的期望有点过了
查看>>
微信小程序wx:key以及wx:key=" *this"详解:
查看>>
下拉框比较符
查看>>
2.2.5 因子的使用
查看>>
css选择器
查看>>
photoplus
查看>>