zoukankan      html  css  js  c++  java
  • [ios]kvo 【转】

    KVO键值观察机制,就是MVC中Model变化的时候通知View更新,使用该机制避免了胶水代码,即在任何数据更新的地方都得写更新UI的更新代码。 具体系统系统:NSObject,NSArray,NSSet三种KVO机制,API如下@interface NSObject(NSKeyValueObserverRegistration)


    /* Register or deregister as an observer of the value at a key path relative to the receiver. The options determine what is included in observer notifications and when they're sent, as described above, and the context is passed in observer notifications as described above.
    */
    - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
    - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;


    @end


    @interface NSArray(NSKeyValueObserverRegistration)


    /* Register or deregister as an observer of the values at a key path relative to each indexed element of the array. The options determine what is included in observer notifications and when they're sent, as described above, and the context is passed in observer notifications as described above. This is not merely a convenience method; invoking this this is potentially much faster than repeatedly invoking -[NSObject(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:].
    */
    - (void)addObserver:(NSObject *)observer toObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
    - (void)removeObserver:(NSObject *)observer fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath;


    /* NSArrays are not observable, so these methods raise exceptions when invoked on NSArrays. Instead of observing an array, observe the ordered to-many relationship for which the array is the collection of related objects.
    */
    - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
    - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;


    @end


    @interface NSSet(NSKeyValueObserverRegistration)


    /* NSSets are not observable, so these methods raise exceptions when invoked on NSSets. Instead of observing a set, observe the unordered to-many relationship for which the set is the collection of related objects.
    */
    - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context NS_AVAILABLE(10_4, 2_0);
    - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath NS_AVAILABLE(10_4, 2_0);


    @end




    只解释@interfaceNSObject(NSKeyValueObserverRegistration)
    - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
    例如:
    Class Model:NSObject

    NSString *text;



    Class Controller:NSObject

    Model *model;

    此时如果在Controller中想使用KVO机制得如下操作
    [model addObserver:self forKeyPath:@“text” options:NSKeyValueObservingOptionNew context:nil];
    这条语句意思是:当model的text属性变化时通知self,其中NSKeyValueObservingOptionNew 可以变换,context参数是void*类型,因此可以传递自己想传递的任何类型。
    除此在Controller中还得实现
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    text属性变化时系统会回调这个函数,因此在这个函数中更新UI。

    - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
    删除观察者记得在释放本对象之前一定要remove observer,比如在dealloc中remove observer。


    - (void)addObserver:(NSObject *)observer toObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
    向数组中某个对象集合添加观察者

  • 相关阅读:
    day67——前后端传输数据的编码格式、ajax传json数据/传文件、批量插入
    day66——choices参数、MTV/MVC模型、三种创建多对多的方式、AJAX
    day65——聚合函数、分组查询、F与Q查询、django开事务、orm查询优化
    dayⅢ、基本数据类型+运算符作业
    dayⅡ:编程语言+变量+垃圾回收制
    dayⅡ:变量作业
    dayⅠ:计算机基础知识
    ⅩⅥ:无参装饰器
    ⅩⅤ:作业
    ⅩⅤ:名称空间与作用域
  • 原文地址:https://www.cnblogs.com/jinjiantong/p/3018364.html
Copyright © 2011-2022 走看看