block使用浅析

  • 实现目标,给键盘添加一个工具条

LZKeyboardTool.xib图:

// 自定义工具条
// LZKeyboardTool.h
#import <UIKit/UIKit.h>

typedef enum {
    KeyboardItemTypePrevious, // 上一个
    KeyboardItemTypeNext, // 下一个
    KeyboardItemTypeDone // 完成
} KeyboardItemType;

// 定义一个类型
typedef void (^myBlock)(KeyboardItemType);

@interface LZKeyboardTool : UIView

+ (instancetype)sharekeyboardTool;

@property (nonatomic, copy) myBlock pBlock;

@end

// LZKeyboardTool.m
#import "LZKeyboardTool.h"

@interface LZKeyboardTool()

@end

@implementation LZKeyboardTool

static LZKeyboardTool* _instance;

+ (void)load
{
    // 创建一个对象
    _instance = [[[NSBundle mainBundle] loadNibNamed:@"LZKeyboardTool" owner:nil options:nil] lastObject];

}
// 单例对象
+ (instancetype)sharekeyboardTool
{
    return _instance;
}

// 上一个
- (IBAction)previous:(id)sender {
    if (_pBlock) { // 先判断
        _pBlock(KeyboardItemTypePrevious); // 调用block
    }
}

// 下一个
- (IBAction)next:(id)sender {
    if (_pBlock) { // 先判断
        _pBlock(KeyboardItemTypeNext); // 调用block
    }
}
// 完成
- (IBAction)done:(id)sender {
    if (_pBlock) { // 先判断
        _pBlock(KeyboardItemTypeDone); // 调用block
    }
}

@end


// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end


// ViewController.m
#import "ViewController.h"
#import "LZKeyboardTool.h"

@interface ViewController ()
{
    NSArray *_fields; // 存储所有的textField
}

// 生日框
@property (weak, nonatomic) IBOutlet UITextField *birthdayField;
// 输入框容器
@property (weak, nonatomic) IBOutlet UIView *inputContainer;
/** LZKeyboard数据*/
@property (nonatomic, strong) LZKeyboardTool *tool;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.初始化自定义键盘
    [self setupCustomKeyboard];

    // 创建自定义键盘
    self.tool = [LZKeyboardTool sharekeyboardTool];

    // 2.设置每一个textfield的键盘工具view(inputAccessoryView)
    [self setupKeyboardTool];

    // 3.监听键盘的事件
    [self setupKeyboardNotification];

    // 含义,弱引用,防止循环引用
    // #define WeakSelf __weak typeof(self) weakSelf = self;
    __weak typeof(self) weakSelf = self;

    // 用block保存一段代码
    self.tool.pBlock = ^ (KeyboardItemType itemType){
        // 获取当前响应者的索引
        int currentIndex = [weakSelf getCurrentResponderIndex];

        switch (itemType) {
            case KeyboardItemTypePrevious:
                NSLog(@"上一个");
                [weakSelf showPreviousField:currentIndex];
                break;
            case KeyboardItemTypeNext:
                [weakSelf showNextField:currentIndex];
                break;
            case KeyboardItemTypeDone:
                [weakSelf touchesBegan:nil withEvent:nil];
                break;
        }

    };

}

// 获取当前textField的响应者索引
// 如果返回-1代理没有找到响应者
- (int)getCurrentResponderIndex
{
    // 遍历所有的textField获取响应者
    for (UITextField *tf in _fields) {
        if (tf.isFirstResponder) {
            return [_fields indexOfObject:tf];
        }
    }
    return -1;
}

// 1.初始化自定义键盘
- (void)setupCustomKeyboard
{
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];

    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    datePicker.datePickerMode = UIDatePickerModeDate;

    self.birthdayField.inputView = datePicker;
}

// 2.设置每一个textfield的键盘工具view(inputAccessoryView)
- (void)setupKeyboardTool
{
    // 创建工具栏
    LZKeyboardTool *tool = [LZKeyboardTool sharekeyboardTool];

    // 1.获取输入框窗口的所有子控件
    NSArray *views = self.inputContainer.subviews;

    // 创建一个数据存储textfield
    NSMutableArray *fieldsM = [NSMutableArray array];

    // 2.遍历
    for (UIView *child in views) {
        // 如果子控制器是UITextField的时候,设置inputAccessoryView
        if ([child isKindOfClass:[UITextField class]]) {
            UITextField *tf = (UITextField *)child; // 类型转换
            tf.inputAccessoryView = tool;
            [fieldsM addObject:tf];
        }
    }

    _fields = fieldsM;

}

- (void)setupKeyboardNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)kbFrameChange:(NSNotification *)notifi
{
//    NSLog(@"%@", notifi);
//    NSLog(@"%@", notifi.userInfo[@"UIKeyboardFrameEndUserInfoKey"]);
    // 获取键盘改变的y值
    // 键盘结束时的fm
    CGRect kbEndFrm = [notifi.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];

    // 键盘结束时的y
    CGFloat kEndY = kbEndFrm.origin.y;

    // 获取当前的响应者
    int currentIndex = [self getCurrentResponderIndex];
    UITextField *currentTf = _fields[currentIndex];
    int inputY = self.inputContainer.frame.origin.y;
    CGFloat tfMaxY = CGRectGetMaxY(currentTf.frame) + inputY;
    NSLog(@"kEndY = %f, tfMaxY = %f, inputY = %d", kEndY, tfMaxY, inputY);
    // 改变控制器view的transform
    if (tfMaxY > kEndY) {
        self.view.transform = CGAffineTransformMakeTranslation(0, kEndY - tfMaxY);
    }else{
        [UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformIdentity; // 恢复到原来位置
        }];
    }

}

#pragma mark -键盘工具条的代理

// 让上一个field成为响应者
- (void)showPreviousField:(int) currentIndex{
    int previousIndex = currentIndex - 1;
    if (previousIndex >= 0) {
        UITextField *previousTf = [_fields objectAtIndex:previousIndex];
        [previousTf becomeFirstResponder];
    }
}

- (void)showNextField:(int) currentIndex{
    int nextIndex = currentIndex + 1;
    if (nextIndex < _fields.count) {
        UITextField *nextTf = [_fields objectAtIndex:nextIndex];
        [nextTf becomeFirstResponder];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];

    [UIView animateWithDuration:0.25 animations:^{
        self.view.transform = CGAffineTransformIdentity; // 恢复到原来位置
    }];
}

@end

效果图片:

  • 笔者主要是想通过该示例程序来说明一下笔者使用block的步骤 - 在LZKeyboardTool.h里面定义一个block类型,并且定义了一个pBlock变量

    // 定义一个类型
    typedef void (^myBlock)(KeyboardItemType);
    
    @interface LZKeyboardTool : UIView
    
    + (instancetype)sharekeyboardTool;
    
    @property (nonatomic, copy) myBlock pBlock;
    
    @end
    
    • 在ViewController.m里面使用pBlock变量保存了一段代码,里面比如把用到的self(指代的是控制器)重新定义了一个弱指针指向了它,为了防止在pBlock保存的代码中使用到self而造成循环引用,这里笔者也是网上百度搜索到的,这里也恰好是重点
    // 含义,弱引用,防止循环引用
    __weak typeof(self) weakSelf = self;
    
    // 用block保存一段代码
    self.tool.pBlock = ^ (KeyboardItemType itemType){
        // 获取当前响应者的索引
        int currentIndex = [weakSelf getCurrentResponderIndex];
    
        switch (itemType) {
            case KeyboardItemTypePrevious:
                NSLog(@"上一个");
                [weakSelf showPreviousField:currentIndex];
                break;
            case KeyboardItemTypeNext:
                [weakSelf showNextField:currentIndex];
                break;
            case KeyboardItemTypeDone:
                [weakSelf touchesBegan:nil withEvent:nil];
                break;
        }
    
    };
    
    • 在LZKeyboardTool.m里面调用pBlock保存的代码
    // 上一个
    -(IBAction)previous:(id)sender {
    if (_pBlock) { // 先判断
        _pBlock(KeyboardItemTypePrevious); // 调用block
    }
    }
    
    • OK,结束,就这么简单
  • 下面在来一个示例代码,不是全部的,主要是为了说明block怎么去使用,首先通过下面这张图片来表达笔者的意图,点击加号按钮或者减号按钮,得控制下面显示的总价格,第一,加号按钮和减号按钮位于tableView上,下面那一个UIView是与tableView分隔开的,也就是他俩不能进行数据的交互,要实现两者之间交互,可以通过 通知,代理, KVO, block,这里笔者就使用了block来实现,主要是为了说明block的用法


// tableView的cell模型

// XMGWineCell.h
#import <UIKit/UIKit.h>
@class XMGWine;

typedef enum {
    WineCellTypePlus,
    WineCellTypeMinus
} WineCellType;

// 定义一个类型
typedef void (^myBlock)(WineCellType);

@interface XMGWineCell : UITableViewCell

/** 酒模型*/
@property (nonatomic, strong) XMGWine *wine;

@property (nonatomic, copy) myBlock pBlock;

@end

// XMGWineCell.m
#import "XMGWineCell.h"
#import "XMGWine.h"
#import "XMGCircleButton.h"

@interface XMGWineCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imageImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet XMGCircleButton *minusButton;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;


@end

@implementation XMGWineCell

- (void)setWine:(XMGWine *)wine
{
    _wine = wine;
    self.imageImageView.image = [UIImage imageNamed:wine.image];
    self.nameLabel.text = wine.name;
    self.moneyLabel.text = [NSString stringWithFormat:@"¥%@", wine.money];

    self.countLabel.text = [NSString stringWithFormat:@"%zd", wine.count];
//    self.minusButton.enabled = (wine.count > 0);
}

/**
 *  -
 */
- (IBAction)minusClick {
    // 设置模型数据-1
    self.wine.count--;
    // 判断r
    if (self.wine.count == 0) { // 减号不能点击
        self.minusButton.enabled = NO;
    }
    // 赋值
    self.countLabel.text = [NSString stringWithFormat:@"%zd", self.wine.count];

    if (_pBlock) { // 判断
        _pBlock(WineCellTypeMinus); // 调用block
    }
}
/**
 *  +
 */
- (IBAction)plusClick {
    // 设置减号按钮可以点击
    self.minusButton.enabled = YES;
    // 设置模型数据加1
    self.wine.count++;
    // 刷新
    self.countLabel.text = [NSString stringWithFormat:@"%zd", self.wine.count];

    if (_pBlock) { // 判断
        _pBlock(WineCellTypePlus); // 调用block
    }
}


@end


// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

// ViewController.m
#import "ViewController.h"
#import "XMGWineCell.h"
#import "MJExtension.h"
#import "XMGWine.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
/**
 *  购买按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *buyCount;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** 酒模型数组*/
@property (nonatomic, strong) NSArray *wineArray;
/** 总价格*/
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;

/** 购物车对象(存放需要购买的商品) */
@property (nonatomic, strong) NSMutableArray *wineCar;
@end

@implementation ViewController

#pragma mark - 懒加载数据

- (NSMutableArray *)wineCar
{
    if (!_wineCar) {
        _wineCar = [NSMutableArray array];
    }
    return _wineCar;
}

- (NSArray *)wineArray
{
    if (_wineArray == nil) {
        _wineArray = [XMGWine mj_objectArrayWithFilename:@"wine.plist"];
    }
    return _wineArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

#pragma mark - XMGWineCellDelegate 方法
- (void)wineCellDidClickPlusButton:(XMGWineCell *)wineCell
{
    // 计算总价
    int totalPrice = self.totalPriceLabel.text.intValue + wineCell.wine.money.intValue;

    // 设置总价
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%zd", totalPrice];

    self.buyCount.enabled = YES;

    // 如果这个商品已经在购物车中,就不用再添加
    if ([self.wineCar containsObject:wineCell.wine]) return;

    // 添加需要购买的商品
    [self.wineCar addObject:wineCell.wine];
}

- (void)wineCellDidClickMinusButton:(XMGWineCell *)wineCell
{
    // 计算总价
    int totalPrice = self.totalPriceLabel.text.intValue - wineCell.wine.money.intValue;

    // 设置总价
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%zd", totalPrice];

    self.buyCount.enabled = totalPrice > 0;

    // 将商品从购物车中移除
    if (wineCell.wine.count == 0) {
        [self.wineCar removeObject:wineCell.wine];
    }
}

/**
 *  购买
 */
- (IBAction)buy{
    // 循环
    for (XMGWine *wine in self.wineArray) {
        if (wine.count) { // 判断
            NSLog(@"[%@]卖了%zd瓶", wine.name, wine.count);
        }
    }
}

/**
 *  清空
 */
- (IBAction)clear {
    // 模型数据
    for (XMGWine *wine in self.wineArray) {
        wine.count = 0;
    }
    // 刷新数据
    [self.tableView reloadData];
    // 设置购买按钮不可点击
    self.buyCount.enabled = NO;
    // 设置总价格totalPriceLabel值为0
    self.totalPriceLabel.text = @"0";
}


#pragma mark - UITableViewDataSource方法
/**
 * 每组多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.wineArray.count;
}

/**
 *  每行显示什么内容
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 去缓存里面找
    // 含义,弱引用,防止循环引用
    __weak typeof(self) weakSelf = self;

    static NSString *ID = @"wine";
    // 如果在block中访问了外界对象,那么就得加上__block修饰符
    __block __weak XMGWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 赋值
    XMGWine *wine = self.wineArray[indexPath.row];
    cell.wine = wine;
    // 设置
    cell.pBlock = ^ (WineCellType itemType){
        int totalPrice = 0;
        switch (itemType) {
            case WineCellTypePlus:
                // 计算总价
                totalPrice = weakSelf.totalPriceLabel.text.intValue + cell.wine.money.intValue;

                // 设置总价
                weakSelf.totalPriceLabel.text = [NSString stringWithFormat:@"%zd", totalPrice];

                weakSelf.buyCount.enabled = YES;
                // 如果这个商品已经在购物车中,就不用再添加
                if ([weakSelf.wineCar containsObject:cell.wine]) return;
                // 添加需要购买的商品
                [weakSelf.wineCar addObject:cell.wine];

                break;

            case WineCellTypeMinus:
                // 计算总价
                totalPrice = weakSelf.totalPriceLabel.text.intValue - cell.wine.money.intValue;

                // 设置总价
                weakSelf.totalPriceLabel.text = [NSString stringWithFormat:@"%zd", totalPrice];

                weakSelf.buyCount.enabled = totalPrice > 0;

                // 将商品从购物车中移除
                if (cell.wine.count == 0) {
                    [weakSelf.wineCar removeObject:cell.wine];
                }

                break;
        }
    };
    // 返回cell
    return cell;
}

@end


  • 下面笔者来说明一下使用block的步骤

    • 在XMGWineCell.h里面定义一个block类型,并且定义一个pBlock变量
    // 定义一个类型
    typedef void (^myBlock)(WineCellType);
    
    @interface XMGWineCell : UITableViewCell
    
    /** 酒模型*/
    @property (nonatomic, strong) XMGWine *wine;
    
    @property (nonatomic, copy) myBlock pBlock;
    
    • 在ViewController.m里面通过pBlock变量来保存一段代码,这句代码__weak typeof(self) weakSelf = self;笔者就不多解释了,主要是这句代码__block __weak XMGWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];很关键,因为pBlock变量保存的代码中用到了外界对象,外界对象得加上__block修饰符才能防止循环引用,就这么简单
    // 去缓存里面找
    // 含义,弱引用,防止循环引用
    __weak typeof(self) weakSelf = self;
    
    static NSString *ID = @"wine";
    // 如果在block中访问了外界对象,那么就得加上__block修饰符
    __block __weak XMGWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 赋值
    XMGWine *wine = self.wineArray[indexPath.row];
    cell.wine = wine;
    // 设置
    cell.pBlock = ^ (WineCellType itemType){
        int totalPrice = 0;
        switch (itemType) {
            case WineCellTypePlus:
                // 计算总价
                totalPrice = weakSelf.totalPriceLabel.text.intValue + cell.wine.money.intValue;
    
                // 设置总价
                weakSelf.totalPriceLabel.text = [NSString stringWithFormat:@"%zd", totalPrice];
    
                weakSelf.buyCount.enabled = YES;
                // 如果这个商品已经在购物车中,就不用再添加
                if ([weakSelf.wineCar containsObject:cell.wine]) return;
                // 添加需要购买的商品
                [weakSelf.wineCar addObject:cell.wine];
    
                break;
    
            case WineCellTypeMinus:
                // 计算总价
                totalPrice = weakSelf.totalPriceLabel.text.intValue - cell.wine.money.intValue;
    
                // 设置总价
                weakSelf.totalPriceLabel.text = [NSString stringWithFormat:@"%zd", totalPrice];
    
                weakSelf.buyCount.enabled = totalPrice > 0;
    
                // 将商品从购物车中移除
                if (cell.wine.count == 0) {
                    [weakSelf.wineCar removeObject:cell.wine];
                }
    
                break;
        }
    };
    // 返回cell
    return cell;
    
    • 调用pBlock变量保存的代码
    
    -(IBAction)plusClick {
    // 设置减号按钮可以点击
    self.minusButton.enabled = YES;
    // 设置模型数据加1
    self.wine.count++;
    // 刷新
    self.countLabel.text = [NSString stringWithFormat:@"%zd", self.wine.count];
    
    if (_pBlock) { // 判断
        _pBlock(WineCellTypePlus); // 调用block
    }
    }
    
    • OK,大功告成
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容

  • Block使用场景,可以在两个界面的传值,也可以对代码封装作为参数的传递等。用过GCD就知道Block的精妙之处。...
    Coder_JMicheal阅读 696评论 2 1
  • 目录 Block概述 Block定义方式 Block保存代码 Block传值 Block对外部变量的传递 Bloc...
    子斌阅读 1,360评论 2 7
  • iOS代码块Block 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,B...
    smile刺客阅读 2,247评论 2 26
  • 在介绍Block之前通过一个简单的应用场景认识下Block 场景描述如下:TableView上面有多个Custom...
    黑_白_灰阅读 1,352评论 4 29
  • block.png iOS代码块Block 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实...
    全栈农民工阅读 569评论 0 1