zoukankan      html  css  js  c++  java
  • kvo

    #import <Foundation/Foundation.h>
    
    @interface NameObServer : NSObject
    
    
    @end
    
    
    #import "NameObServer.h"
    
    @implementation NameObServer
    
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
        NSLog(@"%@",keyPath);
        NSLog(@"%@",object);
        NSLog(@"%@",change);
    }
    @end
    ====================================
    #import <Foundation/Foundation.h>
    
    @interface Student : NSObject
    
    @property(nonatomic,copy) NSString *name;
    
    @end
    
    
    #import "Student.h"
    
    @implementation Student
    
    -(void)dealloc{
        [_name release];
        [super dealloc];
    }
    @end
    
    
    ===================================
    
    #import <Foundation/Foundation.h>
    #import "Student.h"
    #import "NameObServer.h"
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            Student *stu=[[[Student alloc] init] autorelease];
            stu.name=@"mike";
            NameObServer *nameobserve=[[[NameObServer alloc] init] autorelease];
            //在给student的属性name添加观察对象
            [stu addObserver:nameobserve forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
            stu.name=@"jack";
            //移除student的属性 name对象
            [stu removeObserver:nameobserve forKeyPath:@"name"];
        
            
        }
        return 0;
    }
    
  • 相关阅读:
    class11_创建新的输出字段P2
    class10_创建新的输出字段
    class09_高级过滤数据
    Markdown高级语法
    class08_过滤数据
    class07_查询数据
    class06_插入数据
    class05_操纵表
    class04_创建表02
    class03_Create a new table by SQL
  • 原文地址:https://www.cnblogs.com/gcb999/p/3065462.html
Copyright © 2011-2022 走看看