KVO的簡單使用

  • 模型數據

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


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容