KVO What is Kvo-->what
Key-value observation (Key-value observing) is a mechanism. This mechanism allows other objects to be observed when the value of certain properties of an object is modified. The communication between Model and Controller in common language MVC.
How to use KVO
The method of use is divided into three steps:
1. Registration, the properties of the observed object need to be developed
2. Ways to implement Callbacks
3. Remove the observation (run out, don't forget to remove)
Description of common methods and data types
//Add listeners and specify the keypath of the listener, and listen for changes in those values- (void) Addobserver: (NSObject*) Observer Forkeypath: (NSString*) KeyPath options: (nskeyvalueobservingoptions) Options Context: (void*) context;options parameter: Nskeyvalueobservingoptions explains what changes you want to listen to nskeyvalueobservingoptionnew property changes, only the new value is notified Nskeyvalu When the Eobservingoptionold property changes, only the old value is notified Nskeyvalueobservingoptioninitial the callback method is called immediately when the method to add the listener is called, notifying the initial value nskeyvalueobserving Optionprior replaces a single notification, before the value changes, will be called, such as set old, not only notify once, before and after the change will be notified two times.//Remove the Observer object for the specified key path- (void) Removeobserver: (NSObject*) Observer Forkeypath: (NSString*) KeyPath Context: (void*) context;This method is called when the listening property changes, and the value of change stores the corresponding value according to the setting of the listener option.- (void) Removeobserver: (NSObject*) Observer Forkeypath: (NSString*) KeyPath Context: (void*) Context
Note: The value of the property must be modified with the Set method, or KVC method, to cause KVO notification
Instance:
Assuming a person, we are now listening to changes in their age
1. Defining the Model class
@interface QYPerson : NSObject@property (nonatomicstrongNSString *name;@property (nonatomicint age;@end
2. Instantiate a model object, register the Observer, implement the callback for the method, and create the Lable object and button object, display on lable when the age of person changes, and print the corresponding property value in the console
#import "ViewController.h" #import "QYPerson.h" @interface viewcontroller ()@property(nonatomic,Strong) Qyperson *p1;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];UIButton*BTN = [UIButtonButtonwithtype:uibuttontypesystem]; Btn. Frame= CGRectMake ( -, -,135, -); [Btn settitle:@"Change"Forstate:uicontrolstatenormal]; [BTN AddTarget: SelfAction@selector(Changeage) forcontrolevents:uicontroleventtouchupinside]; [ Self. ViewADDSUBVIEW:BTN];UILabel*lable = [[UILabelAlloc] Initwithframe:cgrectmake ( -, -,175, -)]; lable. TextColor= [UicolorOrangecolor]; lable. Tag=101; lable. TextAlignment= Nstextalignmentcenter; [ Self. ViewAddsubview:lable]; Qyperson *P1 = [[Qyperson alloc] init]; P1. Name= @"Zhangsan"; P1. Age= -; _p1 = p1;//1. Register an observer[P1 addobserver: Selfforkeypath:@"Age"options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:NULL];} - (void) changeage{ Self. P1. Age+=1;UILabel*lable = [(UILabel*) Self. ViewViewwithtag:101]; lable. Text= [NSStringstringwithformat:@"%d", Self. P1. Age];}//2. Callback method when the value of the observation changes, the method is called- (void) Observevalueforkeypath: (NSString*) KeyPath Ofobject: (ID) Object change: (nsdictionary*) Change Context: (void*) context{if([KeyPath isequaltostring:@"Age"]) {intNewAge = [change[@"New"] Intvalue];intOldage = [change[@"Old"] Intvalue];NSLog(@"New Age:%d", newage);NSLog(@"old Age:%d", oldage); }}
The printing results and interface changes are as follows:
2015-07-29 13:22:05.636 03-kvodemo[1998:535425] New age:19
2015-07-29 13:22:05.636 03-kvodemo[1998:535425] Old age:18
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS KVO Overview and usage