KVO的简单使用

作者: Z了个L | 来源:发表于2016-07-26 20:22 被阅读82次
  • 模型数据

// XMGWine.h
#import <Foundation/Foundation.h>

@interface XMGWine : NSObject
@property (copy, nonatomic) NSString *money;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *image;

/** 购买的数量*/
@property (nonatomic ,assign) int count;
@end


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

@implementation XMGWine

@end


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

@class XMGWine;
@interface XMGWineCell : UITableViewCell

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

@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 UILabel *countLabel;
@property (weak, nonatomic) IBOutlet XMGCircleButton *minusButton;

@end
@implementation XMGWineCell

- (void)setWine:(XMGWine *)wine
{
    _wine = wine;
    self.imageImageView.image = [UIImage imageNamed:wine.image];

    self.nameLabel.text = wine.name;

    self.moneyLabel.text = wine.money;

    // 根据count决定countLabel显示的文字
    self.countLabel.text = [NSString stringWithFormat:@"%zd",wine.count];
    // 根据count决定减号是否能点击
    self.minusButton.enabled = (wine.count > 0);
}

// KVO

/**
 * 加号点击
 */
- (IBAction)plusClick {
    // 修改模型
    self.wine.count ++ ;
    // 修改界面
    self.countLabel.text = [NSString stringWithFormat:@"%d",self.wine.count];
    // 减号按钮一定能点击
    self.minusButton.enabled = YES;
}

/**
 *  减号点击
 */
- (IBAction)minusClick {
    // 修改模型
    self.wine.count -- ;
    // 修改界面
    self.countLabel.text = [NSString stringWithFormat:@"%d",self.wine.count];

    // 减号按钮不能点击
    if (self.wine.count == 0) {
        self.minusButton.enabled = NO;
    }

}
@end



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

@interface ViewController : UIViewController

@end

// ViewController.m


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

@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 所有的酒数据*/
@property (nonatomic ,strong) NSArray *wineArray;

/** 总价*/
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;
@property (weak, nonatomic) IBOutlet UIButton *buyButton;

@end

@implementation ViewController


- (NSArray *)wineArray
{
    if (!_wineArray) {
        _wineArray = [XMGWine mj_objectArrayWithFilename:@"wine.plist"];
        // KVO监听
        for (XMGWine *wine in _wineArray) {
            // 监听模型的count属性
            [wine addObserver:self forKeyPath:@"count" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
//            NSLog(@"%@",[wine valueForKeyPath:@"isa"]);
        }
    }
    return _wineArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)dealloc{
    // 移除监听
    for (XMGWine *wine in _wineArray) {
        [wine removeObserver:self forKeyPath:@"count"];
    }
}

#pragma mark - KVO的监听方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(XMGWine *)wine change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    // NSKeyValueChangeNewKey == @"new"
    int new = [change[NSKeyValueChangeNewKey] intValue];
    // NSKeyValueChangeOldKey == @"old"
    int old = [change[NSKeyValueChangeOldKey] intValue];

    if (new > old) { // 点击加号
        // 计算总价
        int totalPrice = self.totalPriceLabel.text.intValue + wine.money.intValue;
        // 设置总价
        self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
        // 购买按钮一点能点击
        self.buyButton.enabled = YES;
    } else { // 点击减号
        // 计算总价
        int totalPrice = self.totalPriceLabel.text.intValue - wine.money.intValue;
        // 设置总价
        self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
        // 控制购买按钮是否能够点击
        self.buyButton.enabled = (totalPrice > 0);
    }

}

#pragma mark - 按钮的点击事件
- (IBAction)clear {
    // 修改模型
    for (XMGWine *wine in self.wineArray) {
        wine.count = 0;
    }

    // 刷新表格
    [self.tableView reloadData];

    // 清空总价
    self.totalPriceLabel.text = @"0";

    // 购买按钮一定不能点击
    self.buyButton.enabled = NO;
}

- (IBAction)buy {
    // 打印用户购买的商品的
    for (XMGWine *wine in self.wineArray) {
        if (wine.count > 0) {
            NSLog(@"购买了%d件%@",wine.count,wine.name);
        }
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.wineArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 访问缓存池
    static NSString *ID = @"wine";
    XMGWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 设置数据
    cell.wine = self.wineArray[indexPath.row];
    return cell;
}
@end


相关文章

  • iOS-KVO

    一.kvo使用 kvo可以监听一个对象属性的变化,下面为简单使用. 二.使用runtime分析kvo 我写了个简单...

  • KVO的使用及底层探究

    KVO的使用 KVO使用起来非常简单,三个步骤就搞定啦 1、通过addObserver: forKeyPath: ...

  • KVO(一)KVO的简单使用

    在开发过程中,经常会对某一个值进行特定的处理,比如一个按钮是否被选中等等。这时候就会用到KVO。所谓KVO简单来讲...

  • 小码哥底层原理笔记:KVO的本质

    使用KVO主要是监听属性的变化。简单的KVO如下: KVO的本质 当我们给person1增加KVO后修改age的值...

  • KVO的简单使用

    KVO即“键值监听”,通常需要三步: 1、添加监听对象【addObserver: forKeyPath: opti...

  • KVO的简单使用

    模型数据

  • 关情纸尾---UIKit基础-简述KVC和KVO

    ♥概述 ♥键值编码KVC ♥点语法和KVC ♥ 键值监听KVO ♥KVO的使用步骤也比较简单:

  • iOSKVO的使用

    KVO的使用非常简单,使用KVO的要求是对象必须能支持kvc机制——所有NSObject的子类都支持这个机制。拿上...

  • iOS底层原理-KVO&KVC

    KVO&KVC KVO:键值监听,可以用于监听某个对象属性值的改变 以上是最简单的KVO的使用,通过打印perso...

  • iOS -KVO

    KVO-键值观察 KVC:对象取值或者设置值。KVO:监听对象值的变化。 响应式编程的一种。KVO的使用非常简单,...

网友评论

    本文标题:KVO的简单使用

    本文链接:https://www.haomeiwen.com/subject/hiznjttx.html