UIButton的两种block传值方式

UIButton的两种block传值方式

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

UIButton的两种block传值方式

 

方式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

UIButton的两种block传值方式