Objective-c KVC and KVO and 通知

一、  目标:1、会使用 KVC KVO  2、了解 通知

1、KVC:字符串 表征机制  KVC可以用来访问和设置属性的值。

2、任何对象都可以 进行键值 编码。

3、KVO:当对象 属性值 发生变化时,我们收到 一个通知。

4、NSObject 类为所有对象提供了一个自动观察能力的机制。

  实现监听步骤:

      1)、注册监听对象;

      2)、实现监听方法

      3)、移除监听

二、通知

1、是一种设计模式;

2、MVC间的信息传递,像设置页面APP 皮肤;

3、内部实现机制由 Cocoa 框架支持;

4、通知中心、获取通知中心、发送通知;

5、系统通知、自定义通知;

三、代码:

**************************** Hero.m  ************************************

//

//  Hero.m

//  KVCandKVO

//

//  Created by  on 12-12-25.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import "Hero.h"


@implementation Hero

@synthesize name = _name,ph = _ph;


-(id)initWithName:(NSString *)name andPch:(NSInteger)ph

{

    if (self = [super init]) {

//        _name = [name retain];

//        _ph = ph;

        //    [self setValue:name forKey:@"name"];  这些方法是  NSObject 的方法


//        // KVC KVO 

//        [self setValue:name forKey:@"_name"];         // key 可以是 属性名 或者是 成员变量名

//        [self setValue:[NSNumber numberWithInteger:ph] forKey:@"_ph"];  // 

//        

        //self setValue:@"野蛮人" forKeyPath:@"hero.Yemanren.name"

        

        //现实监听步骤  1 注册监听 2 实现 监听 方法  3 移除监听

        self.name = name;

        self.ph = ph;

    }

    return self;

}

-(void)setPh:(NSInteger)ph

{

    _ph = ph;   

    NSLog(@"ph = %ld",_ph);

    

//    NSNotification * notic = [NSNotification notificationWithName:@"Hero will be deading..." object:self];

//    [[NSNotificationCenter defaultCenter]postNotification:notic];


    // 上面 两句话 相当于 下面的一句话 

    // 发送 通知 

    [[NSNotificationCenter defaultCenter]postNotificationName:@"Hero will be deading..." object:self userInfo:nil];

    

}

// 自己 作为 监听者 实现的方法

//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

//

//{

//    NSLog(@"keyPath = %@",keyPath);

//    NSLog(@"object = %@",object);

//    NSLog(@"change = %@",change);

//    NSLog(@"context= %@",context);

//    

//}

-(void)dealloc

{

   // [self removeObserver:self forKeyPath:@"_name"]; // 移除监听 

    

    [_name release];

    [super dealloc];

}

@end


******************************************  Hero.h *********************

//

//  Hero.h

//  KVCandKVO

//

//  Created by  on 12-12-25.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Hero : NSObject

{

    NSString * _name;

    NSInteger _ph;

}

@property(retain,nonatomic)NSString * name;

@property(nonatomic)NSInteger ph;

-(id)initWithName:(NSString *)name andPch:(NSInteger)ph;


-(void)setPh:(NSInteger)ph;


@end


********************************Medicine.h *************************************

#import <Foundation/Foundation.h>


@interface Medicine : NSObject


@end


****************************************Medicine.m***********************************

//

//  Medicine.m

//  KVCandKVO

//

//  Created by  on 12-12-25.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import "Medicine.h"

#import "Hero.h"

@implementation Medicine


// 监听者 实现 NSObject提供的 监听 方法  :一旦 被监听 的属性值 发生变化,系统自己调用该方法

//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

//{

//    

//    NSLog(@"keyPath = %@",keyPath);

//    NSLog(@"object = %@",object);

//    NSLog(@"change = %@",change);

//    NSLog(@"context= %@",context);

//    if ([[change valueForKey:NSKeyValueChangeNewKey]integerValue] == 0) {

//        [object setValue:[NSNumber numberWithInteger:500] forKeyPath:@"_ph"];

//        NSLog(@"context= %@",context);

//

//    }

//}


// 通知 要执行的 方法

-(void)doChange:(NSNotification *)not

{

    NSString * name = [not name];

    NSDictionary * dict = [not userInfo];

    Hero * object = (Hero*)[not object];

    NSLog(@"hero 血值 被改变了 。。。");

    NSLog(@"name = %@",name);

    NSLog(@"dict = %@",dict);

    NSLog(@"object = %@",object);

    if (object.ph == 0) {

        object.ph = 500;

    }

    

}

@end


*************************************************Main.m********************

//

//  main.m

//  KVCandKVO

//

//  Created by  on 12-12-25.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Hero.h"

#import "Medicine.h"

int main(int argc, const char * argv[])

{


    @autoreleasepool {

        

//        Hero * hero = [[Hero alloc]initWithName:@"cimayi" andPch:30];

//        NSLog(@"name = %@,ph = %ld",[hero valueForKey:@"_name"],[[hero valueForKey:@"_ph"] integerValue]);

//        

//        NSString * str = @" 掉血了。。。";

//        Medicine * medicine = [[Medicine alloc]init];

//        // 注册监听 监听者是 medicine

//        [hero addObserver:medicine forKeyPath:@"_ph" options:NSKeyValueObservingOptionNew context:str];

//        NSInteger ph = 1000;

//        for (int i=0; i<10; i++) {

//            ph = ph -100;

//            // 被监听 属性值 发生 变化 

//            [hero setValue:[NSNumber numberWithInteger:ph] forKeyPath:@"_ph"];

//            

//            sleep(1); // 休眠 一秒钟

//        }

//        

//        // 英雄 自己监听 自己 成员变量 _name 

//        [hero addObserver:hero forKeyPath:@"_name" options:NSKeyValueObservingOptionNew context:nil];

//        [hero setValue:@"aaaaaa" forKeyPath:@"_name"]; // 

//        

//        [hero removeObserver:medicine forKeyPath:@"_ph"]; //移除监听  

//    

        

        // **********************************  通知 ***************************

        // 1 注册 通知 2、发送通知 3、移除通知

        Hero * hero = [[Hero alloc]initWithName:@"xiaoxue" andPch:22];

        Medicine * med = [[Medicine alloc]init];

        // 注册监听  通知中心 注册 ,同时 指定 所要监听的通知名和监听通知后要执行的 操作 通知发送者 

        [[NSNotificationCenter defaultCenter] addObserver:med selector:@selector(doChange:) name:@"Hero will be deading..." object:hero];

        

        NSInteger ph =1000;

        for (int i = 0; i<10; i++) {

            ph = ph -100;

            hero.ph = ph;

            sleep(1);

        }

        

        

        // 移除 通知

        [[NSNotificationCenter defaultCenter]removeObserver:med name:@"Hero will be deading..." object:hero];

    }

    


    return 0;

}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值