IOS Development Note-objective-c KVC, KVO

Source: Internet
Author: User

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:

    1. by AddObserver:forKeyPath:options:context: Registering listeners for the listener (which is usually the data model)
    2. Rewrite the listener's ObserveValueForKeyPath:ofObject:change:context: method
    3. 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

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.