KVO mode of OC

Source: Internet
Author: User

Key-Value Observing (KVO): This mechanism allows an object to receive notifications when the attributes of the specified object are modified. Each time the attribute of the object to be observed is modified, KVO automatically notifies the corresponding observer. Because the KVO mode is supported by the framework, developers do not need to design their own observer mode, and do not need to add additional code for ease of use. How KVO mode works: If object A wants to receive A notification when A specific attribute of object B changes, it needs to use addObserver: forKeyPath: options: context for registration. The addObserver: forKeyPath: options: context: "method establishes a connection between the specified object instance. This connection is not established between two classes, but between two object instances. In addition, to respond to messages, object A must implement the "observeValueForKeyPath: ofObject: change: context:" method. This method is used to respond to changed messages. In this method, we can follow our own situation to implement the corresponding logic to cope with the property changes of the observed object. In this way, when the specific attribute of object B changes, the logic in the observeValueForKeyPath: ofObject: change: context method in object A will be called back to implement the KVO mode process. The sample code is as follows: [cpp]-(void) viewDidLoad {[super viewDidLoad]; _ label = [[UILabel alloc] initWithFrame: CGRectMake (20, 50, 50, 30)]; _ label. textAlignment = NSTextAlignmentCenter; _ label. font = [UIFont systemFontOfSize: 20]; [self. view addSubview: _ label]; [self addObserver: self forKeyPath: @ "textvalue" options: 0 context: nil]; // register [self setValue: [NSNumber numberWithInt: 2] forKey: @ "textvalue"];} // listener callback-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {if ([keyPath isw.tostring: @ "textvalue"]) {NSNumber * num = [self valueForKey: @ "textvalue"]; _ label. text = [NSString stringWithFormat: @ "% d", [num integerValue] ;}} shent
 

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.