zoukankan      html  css  js  c++  java
  • 手动设定实例变量的KVO实现监听

    手动设定实例变量的KVO实现监听

    如果将一个对象设定成属性,这个属性是自动支持KVO的,如果这个对象是一个实例变量,那么,这个KVO是需要我们自己来实现的.

    以下给出源码供君测试:

    Student.h 与 Student.m

    //
    //  Student.h
    //  SuperNotification
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Student : NSObject
    
    {
        NSString  *_age;
    }
    - (void)setAge:(NSString *)age;
    - (NSString *)age;
    
    @property (nonatomic, strong) NSString  *name;
    
    @end
    //
    //  Student.m
    //  SuperNotification
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "Student.h"
    
    @implementation Student
    
    @synthesize name = _name;
    - (void)setName:(NSString *)name
    {
        _name = name;
    }
    - (NSString *)name
    {
        return _name;
    }
    
    
    // 手动设定KVO
    - (void)setAge:(NSString *)age
    {
        [self willChangeValueForKey:@"age"];
        _age = age;
        [self didChangeValueForKey:@"age"];
    }
    - (NSString *)age
    {
        return _age;
    }
    + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
    {
        // 如果监测到键值为age,则指定为非自动监听对象
        if ([key isEqualToString:@"age"])
        {
            return NO;
        }
        
        return [super automaticallyNotifiesObserversForKey:key];
    }
    
    @end

    具体使用情况:

    //
    //  RootViewController.m
    //  SuperNotification
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "RootViewController.h"
    #import "Student.h"
    
    @interface RootViewController ()
    
    @property (nonatomic, strong) Student *student;
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // 创建学生对象
        _student = [Student new];
        
        // 监听属性name
        [_student addObserver:self
                   forKeyPath:@"name"  // 属性
                      options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                      context:nil];
        
        // 监听实例变量age
        [_student addObserver:self
                   forKeyPath:@"age"   // 实例变量
                      options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                      context:nil];
        
        _student.name = @"YouXianMing"; // 改变名字
        _student.age  = @"18";          // 改变年龄
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(id)object
                            change:(NSDictionary *)change
                           context:(void *)context
    {
        NSLog(@"%@", change);
    }
    
    @end

    这个是手动实现KVO需要注意的地方:

  • 相关阅读:
    wzplayer2 支持mac 了,最新谍报
    关于duilib的理解
    DMS的实现转载
    视频通话最新谍报
    新人补钙系列教程之:Function类的重要方法apply()
    新人补钙系列教程之:webgame好友模块原型开发一
    新人补钙系列教程之: 大型 webGame 开发系列之 pipes
    新人补钙系列教程之:模拟java多线程Thread类
    flash学习网站
    新人补钙系列教程之:AS3与服务器通信
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3902827.html
Copyright © 2011-2022 走看看