IOS development-KVO, ios-kvo

Source: Internet
Author: User

IOS development-KVO, ios-kvo
1. What is kvo?

Key-value observing, observer Mode

Observer: Observe the changes of Object Attributes. when the property is changed by the observer, the observer receives a notification and can perform corresponding processing in the callback function.

 

2. What is the function?

The change processing operation can be performed in the same function. Previously, I used to call subsequent operations in the place where the attribute value was modified each time. This operation is cumbersome and involves many modifications, now you only need to operate in the same function.

With kvo, you only need to perform monitoring, making it easier to use and reducing code logic.

 

Iii. application scenarios:

When a property of a control needs to be modified, kvo is suitable. If the property changes, a message is sent to the observer and corresponding operations are performed in the callback function.

 

4. Example: 1) Explanation: 
Typedef NS_OPTIONS (NSUInteger, NSKeyValueObservingOptions) {items = 0x01, // The value after the change is success = 0x02, // change the value before using NS_ENUM_AVAILABLE (10_5, 2_0) = 0x04, // After addobserving, The observeValueForKeyPath will be called immediately, and the NSKeyValueObservingOptionPrior NS_ENUM_AVAILABLE (10_5, 2_0) = 0x08 // be called twice. Before and after the value changes };

NSKeyValueObservingOptionNew = 0x01, // The value after the change
NSKeyValueObservingOptionOld = 0x02, // change the previous value
These two methods are used more often.

 

NSObject (NSKeyValueObserving) // once the attributes of the observer change, this method will be called for subsequent operations in this method-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context;

KeyPath: The property of the observed object, represented by a string

Object: observed object

Change: the value of a property change. It is a dictionary and is used by objectForKey (key is

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 );

Corresponds to the NSKeyValueObservingOptions specified by addobserving

)

Context: The data to be transmitted (void *: Any pointer type). Generally, it is passed (_ bridgevoid *) self or nil. You can also transmit other data.

 

For example:

-(Void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {if (context = (_ bridge void *) self) {if ([keyPath isinclutostring: kKeyPathForNavigationItemRightBarButtonItems]) {// value: NSArray * rightBarButtonItems = [change objectForKey: NSKeyValueChangeNewKey]; // self. navigationItem. rightBarButtonItems = rightBarButtonItems;} else {[super observeValueForKeyPath: keyPath ofObject: objectchange: changecontext: context];}

 

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

(2) interface methods
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 encryption (10_7, 5_0);-(void) removeObserver :( NSObject *) observer forKeyPath :( NSString *) keyPath; NSArray (custom)-(void) addObserver :( NSObject *) observer toObjectsAtIndexes :( NSIndexSet *) indexes forKeyPath :( NSString *) keyPath options :( optional) options context :( void *) context; -(void) removeObserver :( NSObject *) observer fromObjectsAtIndexes :( NSIndexSet *) indexes forKeyPath :( NSString *) keyPath context :( void *) values (10_7, 5_0);-(void) removeObserver :( NSObject *) observer configure :( NSIndexSet *) indexes forKeyPath :( NSString *) keyPath;-(void) addObserver :( NSObject *) observer forKeyPath :( NSString *) keyPath options :( handle) 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 (plaintext)-(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) // these methods are used for manual notification-(void) willChangeValueForKey :( NSString *) key;-(void) Comment :( 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 :( encrypted) mutationKind usingObjects :( NSSet *) objects;-(void) returns :( NSString *) key withSetMutation :( partial) mutationKind usingObjects :( NSSet *) objects; NSObject (object) + (NSSet *) keyPathsForValuesAffectingValueForKey :( NSString *) keyNS_AVAILABLE (10_5, 2_0); + (BOOL) automaticallyNotifiesObserversForKey :( NSString *) key; 
**AddObserver and removeObserver must appear in pairs.

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

  ** Manual notification:  There are two ways to notify the observer: automatic notification and manual notification. As the name suggests, a manual notification must call the willChangeValueForKey: And didChangeValueForKey: Method to notify the caller when the value changes. For ease of use, we generally use automatic notifications.

To use manual notifications, you must explicitly inform cocoa in the automaticallyNotifiesObserversForKey method of which key values should be manually notified:

ForExample:

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

This will call

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;  
 
// Re-implement the automaticallyNotifiesObserversForKey method in the NSObject class. If yes is returned, automatic notification is returned. + (BOOL) automaticallyNotifiesObserversForKey :( NSString *) key {// when the two values change, the registered observer is automatically notified. The observer must implement observeValueForKeyPath: ofObject: change: context: method if ([key isEqualToString: @ "frame"]) {return NO;} return [super automaticallyNotifiesObserversForKey: key];} at this time, frame must be manually notified.

* Manual notifications are generally not used. For convenience, all notifications are automatically sent, so you can know this part.

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

The preceding interface methods describe NSObject, NSArray, and NSSet all implement the above methods. Therefore, we can not only observe common objects, but also observe arrays or combine class objects.

Generally, we use an NSObject attribute to observe NSArray. The NSSet attribute of each model in NSArray is similar to that in NSArray, but NSSet is a disordered set.
Infinite interconnection iOS development video tutorial: 94 kvo classroom exercises-What are seeds of Chen Wei mov? Grateful

Infinite interconnection iOS development video tutorial: 9.4 kvo classroom exercises-Chen Wei. mov seeds:
Thunder: // combined + R6KeG6aKR5pWZ56iL77yaOS40IGt2b + ivvuWggue7g + Signature =
Have a good time watching the movie ~

You can send a video tutorial on iOS development: 93kvo concept and usage-Chen Wei [byeomov seed or download link?

IOS development video tutorial: 9.3.kvo concepts and usage-Chen Wei [byeo .. mov seed:
Thunder: // QUFodHRwOi8vYWlrYW5keS5vcmcvaU9T5byA5Y + R6KeG6aKR5pWZ56iL77yaOS4zLmt2b + eahOamguW/teWSjOS9v + logs/logs
This is what you want. If you are satisfied, remember to adopt it. <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.