Key-Value Observing Quick Start Case Study

Source: Internet
Author: User


1) create a console output project:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HLQ9-0.png "style =" float: none; "title =" QQ20130828-13.png "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HM628-1.png "style =" float: none; "title =" QQ20130828-14.png "/>


2) Add two new classes: BankObject and PersonObject. Note that the parent class is NSObject.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HI431-2.png "style =" float: none; "title =" QQ20130828-15.png "/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HH211-3.png "style =" float: none; "title =" QQ20130828-16.png "/>


3) Add the following attributes to the BankObject header file and source file:

@property int accountBalance;


@synthesize accountBalance;

4) Add a response function to the PersonObject source file.

- (void)ob

The following prompt is displayed. Click Enter.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HKO6-4.png "style =" float: none; "title =" QQ20130828-17.png "/>


5) as a Demo, We can output the modified value in this function. The NSLog statement added in this function:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HJT2-5.png "style =" float: none; "title =" QQ20130828-18.png "/>


6) switch to the main. m file, add the following code, and introduce the header file.

#import "BankObject.h"#import "PersonObject.h"

Comment out the original output statement and add the following code:

BankObject * bankInstance = [[BankObject alloc] init]; PersonObject * personInstance = [[PersonObject alloc] init]; /** @ brief register the personInstance as the observer of the accountBalance attribute */[bankInstance addObserver: personInstance forKeyPath: @ "accountBalance" options: NSKeyValueObservingOptionNew context: NULL]; bankInstance. accountBalance = 100; bankInstance. accountBalance = 200; bankInstance. account balance = 300;

7) switch to the output of the PersonObject. m source file and place the breakpoint. When you run the program, the program jumps to the breakpoint,

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HK607-6.png "style =" float: none; "title =" QQ20130828-21.png "/>


8) Click 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HI637-7.png "title =" QQ20130828-23.png "/> to step down, the new value of the output is 100.


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HG004-8.png "style =" float: none; "title =" QQ20130828-22.png "/>





Reference connection: http://www.cocoadev.cn/Objective-C/Key-Value-Observing-Quick-Start-cn.asp

This article from the "Jiangshan wind and rain" blog, please be sure to keep this source http://965678322.blog.51cto.com/4935622/1284103

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.