首页 > 代码库 > IOS开发-KVO

IOS开发-KVO

一、什么是kvo?

key-value observing,观察者模式

观察者,观察对象属性的变化,当被观察者该属性发生变化时,观察者会接收到通知,可以在回调函数中做相应的处理

 

二、有什么作用?

变化处理操作可以在同一个函数中进行,先前本人都会在每次修改属性值的地方调用后续操作,比较繁琐,修改的地方也比较多,现在只要在同一个函数中操作就可以

用kvo只要做监控就行,更加方便易用,减少代码逻辑

 

三、使用场景:

当一个控件某个属性变化需要做别的相应操作时,比较适合用kvo,只要当该属性发生变化时,会发消息给观察者,在回调函数中做相应的操作

 

四、实际例子:

一)解释方法: 

typedef NS_OPTIONS(NSUInteger, NSKeyValueObservingOptions) {      NSKeyValueObservingOptionNew = 0x01,//改变后的值      NSKeyValueObservingOptionOld = 0x02,//改变前的值      NSKeyValueObservingOptionInitial NS_ENUM_AVAILABLE(10_5, 2_0) = 0x04, //addobserving之后会马上调用observeValueForKeyPath,不会等到值改变      NSKeyValueObservingOptionPrior NS_ENUM_AVAILABLE(10_5, 2_0) = 0x08   //分2次调用。在值改变之前和值改变之后  };  

NSKeyValueObservingOptionNew = 0x01,//改变后的值
NSKeyValueObservingOptionOld = 0x02,//改变前的值
这两个用到的比较多

 

NSObject(NSKeyValueObserving)  //一旦被观察者属性发生改变,就会调用此方法后续操作在这个方法中进行  - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; 

keyPath:是被观察对象的属性,字符串表示

object:被观察对象

change:属性改变的值,字典,通过 objectForKey (key为

FOUNDATION_EXPORT NSString *const NSKeyValueChangeKindKey;

FOUNDATION_EXPORT NSString *const NSKeyValueChangeNewKey;

FOUNDATION_EXPORT NSString *const NSKeyValueChangeOldKey;

FOUNDATION_EXPORT NSString *const NSKeyValueChangeIndexesKey;

FOUNDATION_EXPORT NSString *const NSKeyValueChangeNotificationIsPriorKey NS_AVAILABLE(10_5, 2_0);

对应addobserving指定的NSKeyValueObservingOptions

context:需要传输的数据(void *:任意指针类型),一般传(__bridgevoid*)self 或者 nil,用户也能传别的

 

for example:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {     if (context == (__bridge void*)self) {          if ([keyPath isEqualToString:kKeyPathForNavigationItemRightBarButtonItems]) {              //取值              NSArray *rightBarButtonItems = [change objectForKey:NSKeyValueChangeNewKey];              //需要做操作              self.navigationItem.rightBarButtonItems = rightBarButtonItems;          }      }     else {          [super observeValueForKeyPath:keyPath ofObject:objectchange:changecontext:context];      }  } 

 

--------------------------------------------

二)接口方法

NSObject(NSKeyValueObserverRegistration)    - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;  - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context NS_AVAILABLE(10_7, 5_0);  - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;    NSArray(NSKeyValueObserverRegistration)  - (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 context:(void *)contextNS_AVAILABLE(10_7,5_0);  - (void)removeObserver:(NSObject *)observer fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath;  - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;  - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context NS_AVAILABLE(10_7, 5_0);  - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;    NSOrderedSet(NSKeyValueObserverRegistration)  - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;  - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context NS_AVAILABLE(10_7, 5_0);  - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;      NSSet(NSKeyValueObserverRegistration)  - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;  - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context NS_AVAILABLE(10_7, 5_0);  - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;      NSObject(NSKeyValueObserverNotification)  //这些方法都为了手动通知用到  - (void)willChangeValueForKey:(NSString *)key;   - (void)didChangeValueForKey:(NSString *)key;  - (void)willChange:(NSKeyValueChange)changeKind valuesAtIndexes:(NSIndexSet *)indexes forKey:(NSString *)key;  - (void)didChange:(NSKeyValueChange)changeKind valuesAtIndexes:(NSIndexSet *)indexes forKey:(NSString *)key;  - (void)willChangeValueForKey:(NSString *)key withSetMutation:(NSKeyValueSetMutationKind)mutationKind usingObjects:(NSSet *)objects;  - (void)didChangeValueForKey:(NSString *)key withSetMutation:(NSKeyValueSetMutationKind)mutationKind usingObjects:(NSSet *)objects;      NSObject(NSKeyValueObservingCustomization)  + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)keyNS_AVAILABLE(10_5,2_0);  + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key;  
** addObserver和removeObserver要成对出现

 -----------------------------------------------------------------------

 
**手动通知:
 
有两种通知观察者的方式,自动通知和手动通知。顾名思义,手动通知需要在值变化时调用 willChangeValueForKey:和didChangeValueForKey: 方法通知调用者。为求简便,我们一般使用自动通知。

要使用手动通知,需要在 automaticallyNotifiesObserversForKey方法中明确告诉cocoa,哪些键值要使用手动通知:

forExample:

[self willChangeValueForKey:@"frame"];self.frame = CGRectMake(0,0,320,100);[self didChangeValueForKey:@"frame"];

这时候就会调用

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;  
//重新实现NSObject类中的automaticallyNotifiesObserversForKey:方法,返回yes表示自动通知。  + (BOOL)automaticallyNotifiesObserversForKey:(NSString*)key  {      //当这两个值改变时,使用自动通知已注册过的观察者,观察者需要实现observeValueForKeyPath:ofObject:change:context:方法      if ([key isEqualToString:@"frame"])      {          return NO;      }      return [super automaticallyNotifiesObserversForKey:key];  } 这时候frame就必须要手动通知

*手动通知一般不用,为了方便,都自动通知,所以这部分知道就可以了

 -----------------------------------------------------------------------

上面一些接口方法说明NSObject,NSArray,NSSet均实现了以上方法,因此我们不仅可以观察普通对象,还可以观察数组或结合类对象。

一般用的都是观察NSObject的某个属性
对NSArray进行观察是观察NSArray中每个model的属性
NSSet和NSArray差不多,只不过NSSet是无序集合

IOS开发-KVO