Overview
Key-Value encoding (KVC), key-value-monitoring (KVO) features
Key-Value Monitoring KVO
Key Value Observing (abbreviated as KVO) is actually an observer pattern that makes it easy to separate the view component from the data model, and when the data model's property values change, the view component as the listener is fired, and the listener itself is invoked when fired. To implement KVO in OBJC, the Nskeyvalueobserving protocol must be implemented, but fortunately NSObject has implemented the protocol, so OBJC can be used by almost all KVO objects.
The common methods for using KVO operations in OBJC are as follows:
- Register the listener for the specified key path: addObserver:forKeyPath:options:context:
- Delete the listener for the specified key path: removeobserver:forkeypath,removeObserver:forKeyPath:context:
- Callback Listener: observeValueForKeyPath:ofObject:change:context:
The use of the KVO is also relatively straightforward:
- by AddObserver:forKeyPath:options:context: Registering listeners for the listener (which is usually the data model)
- Rewrite the listener's ObserveValueForKeyPath:ofObject:change:context: method
- Delete the listener for the specified key path: Removeobserver:forkeypath, RemoveObserver:forKeyPath:context:
Example:
1. We create a new account model
Account.h
#import <Foundation/Foundation.h>@interface account:nsobject/* */float balance; @end
2. In creating a new user model
Person.h
#import <Foundation/Foundation.h>@class account ; @interface Person:nsobject /* */*name; /* * * account; @end
3. Use in PERSON.M model
//#import "Person.h"#import "Account.h"@implementation Person- (void) Setaccount: (Account *) account{_account=Account ; [Self.account addobserver:self Forkeypath:@"Balance"options:nskeyvalueobservingoptionnew Context:nil];}- (void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange: (Nsdictionary<nsstring *,ID> *) Change context: (void*) context{if([KeyPath isequaltostring:@"Balance"]) {NSLog (@"keypath=%@,object=%@,newvalue=%.2f,context=%@", KeyPath,Object, [Change Objectforkey:@"New"] floatvalue],context); }}- (void) dealloc{[Self.account removeobserver:self Forkeypath:@"Balance"];//Remove Listener}@end
Viewcontroller is easy.
-(void) viewdidload { [super Viewdidload]; *person = [[Person alloc] init]; @" Jiangys " ; = [[Account alloc] init]; = _account; - // Monitor Active }
However, this method cannot be updated to the interface. It is possible to use this scenario, such as pausing, fast-forward, without the need for better interface changes. If you need to change the interface, such as account balance changes, then you need to put in the controller to do the UI update.
////VIEWCONTROLLER.M//KVO////Created by Jiangys on 16/4/19.//copyright©2016 year Jiangys. All rights reserved.//#import "ViewController.h"#import "Person.h"#import "Account.h"@interfaceViewcontroller ()/** Account number*/@property (nonatomic, retain) account*Account ;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; person*person =[[Person alloc] init]; Person.name=@"Jiangys"; _account=[[Account alloc] init]; Person.account=_account; //Increased KVO monitoring[_account addobserver:self Forkeypath:@"Balance"Options:nskeyvalueobservingoptionnew Context:nil]; _account.balance= -; _account.balance= +;}- (void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange: (Nsdictionary<nsstring *,ID> *) Change context: (void*) context{if([KeyPath isequaltostring:@"Balance"]) {NSLog (@"keypath=%@,object=%@,newvalue=%.2f,context=%@", KeyPath,Object, [Change Objectforkey:@"New"] floatvalue],context); }}- (void) dealloc{[_account removeobserver:self Forkeypath:@"Balance"];//Remove Listener}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
KVO source code Download: Http://pan.baidu.com/s/1qYh7y8W
IOS Development Note-objective-c KVC, KVO