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

目录
相关文章
|
JavaScript 开发者 前端开发
组件传值-父组件向子组件传值和 data 与 props 的区别|学习笔记
快速学习组件传值-父组件向子组件传值和 data 与 props 的区别
122 0
组件传值-父组件向子组件传值和 data 与 props 的区别|学习笔记
使用NavHostFragment、navigation--- avtivity向fragment传值,fragment之间传值
使用NavHostFragment、navigation--- avtivity向fragment传值,fragment之间传值
200 0
|
程序员 iOS开发
NSNotification、Delegate、Block和KVO的区别是什么
NSNotification、Delegate、Block和KVO的区别是什么
99 0
自定义tableView的section header/footerView时的view复用问题
自定义tableView的section header/footerView时的view复用问题
323 0
为什么要优先使用copy声明NSString属性?
至于为什么要优先使用copy声明NSString属性?首先科普一下:对象在内存中都有一个入口地址,当我们取到这个对象的地址(也就是指针),可以去改变这个对象的一些属性值,而当我们再次去取这个对象,使用这个对象的属性值(我们已修改过的)时,我们会发现这个值确实变成了我们修改的值,当然,前提是需要这个对象没有被销毁,在内存中还存在。选择使用copy的理由是:NSString属性可能被传入一个NSString实例,也可能是一个NSMutableString实例。当传入了一个NSMutableString实例时,字符串的值可能会在背后悄悄变化。
|
Swift iOS开发
UIActivityIndicatorView的hidden隐藏属性没作用?
现象 创作的UIActivityIndicatorView,想当作一般的 View 使用,然后想隐藏时,把hidden属性设置为 YES;但是一直没有作用。
990 0