KVO, like the Observer pattern, detects the change of an object by setting the observer to the specified object, and when the property of the specified object is modified, the object used as the observer receives a notification. Simply put, the KVO will automatically notify the appropriate observer when the property of the observed object has been modified each time it is specified. The system framework already supports KVO, so it is very convenient to use it directly during the development process.
First, the use of KVO steps |
1, register, specify the attributes of the Observer
2. Implement callback method
3. Removal observation
Second, introduce the usage of KVO by small examples in reality. |
I have a bank card, as long as the amount of money in the card changes, the bank will send a text message to me, the following code using KVO to simulate this process
First create a card bank card class, Cary has money amount of properties, in the initialization of this card, to set an initial value of 1000, and then every 1 seconds to simulate the consumption of 10 yuan, the code is as follows:
Card.h
// Card.h// KVO//// Created by Jerei on 15-6-5. // Copyright (c) 2015 jerehedu. All rights reserved. // #import <Foundation/Foundation.h>@interfacefloat money ; @end
Card.m
//CARD.M//KVO////Created by Jerei on 15-6-5.//Copyright (c) 2015 jerehedu. All rights reserved.//#import "Card.h"@implementationCard-(instancetype) init{if(self =[Super Init]) {_money=1000.0; [Nstimer scheduledtimerwithtimeinterval:1target:self selector: @selector (Changemoney) Userinfo:nil Repeats:yes]; } returnSelf ;}//change the amount in the card-(void) changemoney{Self.money-=Ten; }@end
Then create a people human, in this class has the name name and card bank card two properties.
People.h
//People.h//KVO////Created by Jerei on 15-6-5.//Copyright (c) 2015 jerehedu. All rights reserved.//#import<Foundation/Foundation.h>@classCard;@interfacePeople:nsobject@property (nonatomic, copy) NSString*Name: @property (nonatomic, strong) Card*Card;-(ID) Initwithname: (NSString *) name Andcard: (Card *) card;@end
People.m
//PEOPLE.M//KVO////Created by Jerei on 15-6-5.//Copyright (c) 2015 jerehedu. All rights reserved.//#import "People.h"#import "Card.h"@implementationpeople-(ID) Initwithname: (NSString *) name Andcard: (Card *) card{if(self =[Super Init]) {_name=name; _card=Card; } returnSelf ;}@end
After two classes have been created, the People class is kvo to monitor the change in the amount of the bank card.
1, the person needs to listen to the amount in the bank account, need to register the Listener object
-(void) Addobserver: (NSOBJECT *) Observer Forkeypath: (NSString *) keypath options: ( nskeyvalueobservingoptions) Options Context: (void *) context;
The calling method is the object being listened to, the parameters are:
Addobserver: Viewer
Forkeypath: The properties of the object being monitored, attributes inside it, and the use of dot syntax
Options: What actions to listen to, such as initialization, new values
Context: Contexts, generally nil, when keypath: At the same time, it can be used to differentiate
Therefore, the code that needs to be added in PEOPLE.M is:
-(ID) Initwithname: (NSString *) name Andcard: (Card *) card{ if (self = [Super init]) { = name; = Card; // register, add observer [_card addobserver:self Forkeypath:@ "money" options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold Context:nil]; } return Self ;}
2, once registered, you can go to the callback function to perceive the change process
-(void) Observevalueforkeypath: (NSString *) keypath ofobject: (ID)object Change: ( Nsdictionary *) Change context: (void *) context;
Observevalueforkeypath: String, the property being monitored
Ofobject: Which object is being monitored
Change: A dictionary that contains values before and after changes
Context: Contexts
Therefore, the callback method needs to be implemented in the PEOPLE.M
-(void) Observevalueforkeypath: (NSString *) keypath ofobject: (ID)object Change: ( Nsdictionary *) Change context: (void *) context{ NSLog (@ " callback method%@" , change);}
3. Lifting the Listening object
-(void) Removeobserver: (NSOBJECT *) Observer Forkeypath: (NSString *) KeyPath;
The calling method is the object being listened to
Removeobserver: Viewer
Forkeypath: String
Therefore, the Dealloc method is overridden in the PEOPLE.M to enable the removal of the listener
-(void) dealloc{ [_card removeobserver:self forkeypath:@ "money" ];}
4, Ps:kvo property value changes, can not directly change the value
In the example card class has a Changemoney method, in this method modifies the value of the Listening property, the following introduction 4 method to everyone.
-(void) changemoney{//cannot directly change the value!!!! _money-=Ten;//of the wrong//1. Dot SyntaxSelf.money-=Ten; //2. Set Method floatf = _money-Ten; [Self setmoney:f]; //3. KVC[Self setvalue:@ (f) Forkey:@" Money"]; //4.[Self Willchangevalueforkey:@" Money"]; _money-=Ten; [Self Didchangevalueforkey:@" Money"]; }
If you have any inquiries or technical exchanges, please join the official QQ Group: (452379712)
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
IOS key-value Observing (KVO)