• [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;
    向数组中某个对象集合添加观察者

  • 相关阅读:
    正向代理/反向代理理解、Nginx概述、安装及配置详解
    项目部署问题:xftp无法连接服务器、Nginx403 Forbidden解决、nginx反向代理解决前端跨域问题
    Vue上传文件:ElementUI中的upload实现
    理解Vue的计算属性
    今天在CSDN看懂这个帖子,也是我的困惑,记录一下(过了三十的码农,你选择的是哪个,说出你的想法)
    WCF IIS上部署服务
    [转]WCF RESTful service and WebGrid in ASP.NET MVC 5
    WCF 与其它技术的比较
    Visual Studio Debug和Release的区别及obj的作用
    C# Json格式字符串
  • 原文地址:https://www.cnblogs.com/jinjiantong/p/3018364.html
Copyright © 2020-2023  润新知