OC键值监听(KVO)

Object-C技术学习,更多资源请访问 https://www.itkc8.com

//  main.m

//  OC键值监听(KVO)

//

//  Created by Goddog on 15/1/3.

//  Copyright (c) 2015年 Goddog. All rights reserved.

//

/*

 1.IOS应用程序通常会把应用程序组建分开:数据模型组件和视图组件。数据模型组件的状态数据改变时,视图组件也能动态到更新。

 2.解决方案:(a)利用KVO机制,KVO机制NSKeyValueObserving协议提供支持,该协议有如下方法:

                (1)注册一个监听器用于监听指定的Key路径addObserver:forKeyPath:options:context:

                (2)为key路径删除指定的监听器removeObserver:forKeyPath:

                (3)为key路径删除指定的监听器removeObserver:forKeyPath:context:

            (b)用视图组件监听数据模型组件的改变,当数据模型组件的key路径对应属性值发生改变时,作为监听器的视图组件

               将被激发,激发时就回调监听器自身,方法是:observeValueForKeyPath:ofObject:change:context:

            (c)KVO编程步骤: (1)为被监听对象(通常是数据模型组件)注册监听器。

                           (2)重写监听器的observeValueForKeyPath:ofObject:change:context:  方法。

 */

//
//  Item.h
//  OC键值监听(KVO)
//
//  Created by Goddog on 15/1/3.
//  Copyright (c) 2015年 Goddog. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Item : NSObject
//使用@property定义两个属性
@property (nonatomic,copy) NSString* name;
@property (nonatomic,assign) int price;
@end

 

//
//  Item.m
//  OC键值监听(KVO)
//
//  Created by Goddog on 15/1/3.
//  Copyright (c) 2015年 Goddog. All rights reserved.
//

#import "Item.h"

@implementation Item

@end

 

//
//  ItemView.m
//  OC键值监听(KVO)
//
//  Created by Goddog on 15/1/3.
//  Copyright (c) 2015年 Goddog. All rights reserved.
//

#import "ItemView.h"

@implementation ItemView

@synthesize item = _item;

//实现showItemInfo
-(void) showItemInfo
{
    NSLog(@"item名字是:%@,价格是:%d",self.item.name,self.item.price);
}

//自定义setItem: 方法
-(void) setItem:(Item *)item
{
    self->_item = item;
    
    //为item添加监听器,监听item的name属性和price属性的改变
    [self.item addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    [self.item addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew context:nil];
}

//重写该方法,当被监听的数据模型发生改变时,就会回调监听器的该方法
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"--observeValueForKeyPath方法被调用--");
    //获取修改时所设置的数据
    NSLog(@"被修改的keyPath为:%@",keyPath);
    NSLog(@"被修改的对象为:%@",object);
    NSLog(@"新被修改的属性值为:%@",[change objectForKey:@"new"]);
    NSLog(@"被修改的上下文为:%@",context);
}

//删除监听器
-(void) dealloc
{
    [self.item removeObserver:self forKeyPath:@"name"];
    [self.item removeObserver:self forKeyPath:@"price"];
}

@end


 

//
//  ItemView.m
//  OC键值监听(KVO)
//
//  Created by Goddog on 15/1/3.
//  Copyright (c) 2015年 Goddog. All rights reserved.
//

#import "ItemView.h"

@implementation ItemView

@synthesize item = _item;

//实现showItemInfo
-(void) showItemInfo
{
    NSLog(@"item名字是:%@,价格是:%d",self.item.name,self.item.price);
}

//自定义setItem: 方法
-(void) setItem:(Item *)item
{
    self->_item = item;
    
    //为item添加监听器,监听item的name属性和price属性的改变
    [self.item addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    [self.item addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew context:nil];
}

//重写该方法,当被监听的数据模型发生改变时,就会回调监听器的该方法
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"--observeValueForKeyPath方法被调用--");
    //获取修改时所设置的数据
    NSLog(@"被修改的keyPath为:%@",keyPath);
    NSLog(@"被修改的对象为:%@",object);
    NSLog(@"新被修改的属性值为:%@",[change objectForKey:@"new"]);
    NSLog(@"被修改的上下文为:%@",context);
}

//删除监听器
-(void) dealloc
{
    [self.item removeObserver:self forKeyPath:@"name"];
    [self.item removeObserver:self forKeyPath:@"price"];
}

@end

 

#import <Foundation/Foundation.h>
#import "ItemView.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
     
        //创建Item对象
        Item* item = [[Item alloc] init];
        //设置Item属性
        item.name = @"葵花宝典";
        item.price = 188;
        
        //创建ItemView对象
        ItemView* itemView = [[ItemView alloc] init];
        //将itemView的item属性设置为item;
        itemView.item = item;
        //调用方法
        [itemView showItemInfo];
        
        //再次更改item对象的属性,将会激发监听器的方法
        item.name = @"九阴真经";
        item.price = 199;
        
    }
    return 0;
}

Object-C技术学习,更多资源请访问 https://www.itkc8.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值