IOS: KVO overview and usage

Source: Internet
Author: User
I. Overview

KVO, that is, Key-Value Observing, provides a mechanism. When the attribute of a specified object is modified, the object will receive a notification. In short, KVO automatically notifies the corresponding observer after the attribute of the specified object to be observed is modified.

Ii. Usage

The system framework already supports KVO, so it is very simple for programmers to use.

1. Registration, specifying the properties of the observer,

2. Implement callback Methods

3. Remove observation

Iii. Example:

In this scenario, the stock price is displayed on the current screen. When the stock price changes, the stock price is updated in real time.

1. Define DataModel,

 

[Cpp]View plaincopy

  1. @ Interface StockData: NSObject {
  2. NSString * stockName;
  3. Float price;
  4. }
  5. @ End
  6. @ Implementation StockData
  7. @ End

 

2. Define this model as the Controller attribute, instantiate it, listen to its attributes, and display it in the current View.

 

[Cpp]View plaincopy

  1. -(Void) viewDidLoad
  2. {
  3. [Super viewDidLoad];
  4. StockForKVO = [[StockData alloc] init];
  5. [StockForKVO setValue: @ "searph" forKey: @ "stockName"];
  6. [StockForKVO setValue: @ "10.0" forKey: @ "price"];
  7. [StockForKVO addObserver: self forKeyPath: @ "price" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: NULL];
  8. MyLabel = [[UILabel alloc] initWithFrame: CGRectMake (100,100,100, 30)];
  9. MyLabel. textColor = [UIColor redColor];
  10. MyLabel. text = [stockForKVO valueForKey: @ "price"];
  11. [Self. view addSubview: myLabel];
  12. UIButton * B = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  13. B. frame = CGRectMake (0, 0,100, 30 );
  14. [B addTarget: self action: @ selector (buttonAction) forControlEvents: UIControlEventTouchUpInside];
  15. [Self. view addSubview: B];
  16. }


3. When you click a button, call the buttonAction method to modify the attributes of the object.

 

[Cpp]View plaincopy

  1. -(Void) buttonAction
  2. {
  3. [StockForKVO setValue: @ "20.0" forKey: @ "price"];
  4. }

 

 

4. Implement callback Methods

 

[Cpp]View plaincopy

  1. -(Void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context
  2. {
  3. If ([keyPath isw.tostring: @ "price"])
  4. {
  5. MyLabel. text = [stockForKVO valueForKey: @ "price"];
  6. }
  7. }

 

 

5. Adding observation and canceling observation appear in pairs, so you need to remove the Observer at the end.

 

[Cpp]View plaincopy

  1. -(Void) dealloc
  2. {
  3. [Super dealloc];
  4. [StockForKVO removeObserver: self forKeyPath: @ "price"];
  5. [StockForKVO release];
  6. }

 

 

Iv. SummaryKVO is a simple encoding method. It is applicable to the change of the UIVIew caused by the modification of datamodel. As in the preceding example, after changing the attribute value, the listener will be notified immediately.

Source Network Resources: http://blog.csdn.net/yuquan0821/article/details/6646400

Related Article

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.