KVO iOS Learning-design mode

Source: Internet
Author: User

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

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.