KVO---Key Value Obersving

来源:互联网 发布:网站源码查看 编辑:程序博客网 时间:2024/06/15 19:17

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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 签到后,小白花她在娱乐圈翻红了 江月照 魂飞魄散的上古大仙在修真界诈尸 快穿:大佬她又抢了反派剧本 夫人她总想逃 穿成阴戾反派未来嫂嫂 上神大人他偏甜系啊 咸鱼后妈带崽在综艺当对照组 系统BUG让我成了舔包专业户 快穿:系统有的是力气和手段 当我的霸总老公有了读心术 穿成炮灰渣妻后我和反派开农场 前任小姐姐帅又飒 弄潮1990从厂长开始 光阴之外 心动侵占 重回逃荒开端,手握空间来逆袭 重生之网红教父 蛮荒求生:反派大佬被迫种田 玄学大佬在星际重建地府 总裁又惹火夫人了 农门空间:重生娇娘撩糙汉 重生后我弃了天运之子 快穿:绑定系统后我疯狂崩人设 慕爷你虐错了夫人就是白月光 欢乐班 快穿之反派今天也在求负责 喜提娇夫:快穿作精她被迫崛起了 修真大佬都是我 唐人的餐桌 重生之大小姐她换了黑莲花剧本 重回九零做学霸 穿越星际之直播探险生活 我在古代做美食博主 小机器人每天都在劝我追校草 救命!隐婚老公当着全球撩我 仙子她一心修仙 快穿:主神他好撩人 修真界如今不太平 我与帝君虚情假意后he了 女法医她靠玄学飒翻天