KVO mode ——— Meet the obscene public voyeur mode

Source: Internet
Author: User

A brief talk on the KVO mode-satisfying the wretched man's voyeur mode horse brother KVO mode Key-value observing it provides a mechanism for the object to receive notifications when the properties of the specified object are modified. 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. KVO is actually an application of the "observer" design pattern. My view is that this pattern facilitates decoupling between two classes, especially for the two functions of business logic and view control. My own view: I generally refer to the KVO mode as a peeping mode, very image, because the establishment of an observer, and the use of the callback function, the observer does not know, so I called the KVO mode is the peeping mode, it feels really image. Gossip doesn't say with a very simple OC program to be more intuitive to understand

First create a person class

Contents of the. h file

““

Import Import "BankAccount.h"

@interface person:nsobject{
BankAccount * BANKACCOUNT;
}
Turn on the ability to listen to bank accounts
-(void) registerasobserver;

@end
““

. m file

"'
#import "Person.h"

@implementation person
-(ID) init{
Self =[super init];
if (self) {
BankAccount =[[bankaccount Alloc]init];
}

return self;

}
Openingbalance pointing to his own pointer
static void * Openingbalance = (void *) &openingBalance;
-(void) registerasobserver{
Monitor the change of bank account number after the context followed by a different

[bankAccount addObserver :self forKeyPath:@"openingBalance" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:openingBalance];   /*给银行账号bankAccount增加一个新的监听 self ,监听openingBalance的变化过程  只要openingBalance 有变化,就会调用下面的方法*/

}
This method is the callback function of the listener.
That is to say BankAccount only openingbalance change, will call the following method
KeyPath says the key that was listening was openingbalance.
object represents BankAccount
The change is a dictionary containing the new, old values
Context is a private variable openingbalance
-(void) Observevalueforkeypath: (NSString ) KeyPath ofobject: (ID) object change: (Nsdictionary

Create a bank account class BankAccount.h file

““
#import

. m file

““
#import "BankAccount.h"

@implementation BankAccount
-(ID) init{
Self =[super init];
if (self) {

    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(balanceUpdate) userInfo:nil repeats:YES];}return self;

}
-(void) balanceupdate{
float f =self.openingbalance;
F +=arc4random ()%100;
Remember, you can't write like this, it doesn't change anything.
_openingbalance=f;

//写法一self.openingBalance=f;//写法二//[self setOpeningBalance:f];//写法三 kVC 写法//[self setValue:[NSNumber numberWithFloat:f] forKey:@"openingBalance"];//写法四//[self willChangeValueForKey:@"openingBalance"];//_openingBalance=f;//[self didChangeValueForKey:@"openingBalance"];

}
@end

““

Called in the main function

““
#import

The results of the operation are as follows

2016-05-10 23:32:52.415 kvo[509:11870] How
2016-05-10 23:32:53.421 kvo[509:11870] New value 59, old value 0
2016-05-10 23:32:54.423 kvo[509:11870] New value 66, old value 59
2016-05-10 23:32:55.421 kvo[509:11870] New value 130, old value 66

If you do not understand, please leave a message on my blog, I will be the first time to reply. Brother MA:

More share please follow Weibo it a scattered people

KVO mode ——— Meet the obscene public voyeur 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.