KVO is the abbreviation for Key-value observing key-value observation, simply by observing the change of an object's property value by a key. When the observed attribute changes, the Observer is notified (the observer can be the object itself, or it can be another object).
The following is a simulation of the KVO implementation process:
Create a people class that has two properties name,sex
-----------------------the People.h file----------------------------
#import <Foundation/Foundation.h>
@interface Person: nsobject
-(void) changename; Write a method that changes the name value
@property (nonatomic , copy)nsstring *name; Take name as an example to see the change in the name value
@property (nonatomic , copy)nsstring *sex;
@end
-----------------------the PEOPLE.M file----------------------------
#import "Person.h"
@implementation Person
@synthesize Name,sex;
-(void) ChangeName
{
Self . name = @ " I was watched ";
}
@end
To create an observer Peopleobserve class, in which we observe the change of the name value of the People class, it is very simple to implement a method to
-----------------------the PEOPLEOBSERVE.M file----------------------------
#import "PeopleObserve.h"
@implementation Peopleobserve
Monitor the change in the name of the property in person , this parameter is a dictionary of values before and after the name change is saved.
-(void) Observevalueforkeypath: (nsstring *) keypath ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context
{
if ([KeyPath isequaltostring:@ "name"]) {
Use [change Objectforkey:Nskeyvaluechangeoldkey], sometimes do not prompt objectforkey: knock it out.
NSLog (@ "Changhappen old value:%@ New value:%@", [Change Objectforkey:nskeyvaluechangeoldkey],[ Change Objectforkey:Nskeyvaluechangenewkey]);
}
}
@end
---------the output in the Peopleobserve class is simulated in the Appdelegate entry class when the name value is changed------------
#import "AppDelegate.h"
#import "Person.h"
#import "PeopleObserve.h"
@implementation appdelegate
-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions
{
self. Window = [[uiwindow alloc] initwithframe: [[UIScreen mainscreen] bounds]];
Override point for customization after application launch.
self. Window. backgroundcolor = [Uicolor whitecolor];
[self. Window makekeyandvisible];
objects that are being monitored
Person *p = [[person alloc] init];
P.name = @ " observed "; First assign a value to the name to see
Peopleobserve *po = [[Personmonitor alloc] init];
//P is the Observer , the PO is the Observer, and when the change is observed , the method is called
[P addobserver:p m forkeypath:@ "name" options: Nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold context:nil];
[P changename];
The following two ways can change the value of the name, KVC the value of the way
[P setvalue:@ " I was also observed" forkeypath:@ "name"];
[P setValue:@ " I was observed again " Forkey:@ "name"];
return YES;
}
---------------The result of the output-----------------
KVO iOS Learning-design mode