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需要注意的地方:

  • 相关阅读:
    winfrom 正则验证
    winfrom api发送消息,实现滚动效果(重绘滚动条用)
    C#程序员开发WinForm必须知道的 Window 消息大全
    C# VS EditorBrowsable特性 控制智能提示
    C# Attribute简介
    Win32窗口鼠标消息
    C# 自定义控件容器,设计时可添加控件
    Linux管线命令
    Linux数据流重定向
    Shell中的变量
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3902827.html
Copyright © 2011-2022 走看看