KVO一瞥
Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects. It is particularly useful for communication between model and controller layers in an application. (In OS X, the controller layer binding technology relies heavily on key-value observing.) A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller. In addition, however, a model object may observe other model objects (usually to determine when a dependent value changes) or even itself (again to determine when a dependent value changes).
KVO提供了一種機(jī)制,允許對象收到其他對象的特定屬性改變的通知。
在應(yīng)用程序中模型和控制器層之間的通信尤其有用。(在OS X中,控制器層綁定技術(shù)嚴(yán)重依賴KVO)??刂破鲗ο笸ǔS^察模型對象的屬性,視圖對象通過控制器觀察模型對象的屬性。另外,然而,一個模型對象可以觀察其他模型對象(通常用來確定依賴值何時改變)甚至自己(再次確定依賴值何時改變)。
You can observe properties including simple attributes, to-one relationships, and to-many relationships. Observers of to-many relationships are informed of the type of change made—as well as which objects are involved in the change.
你可以觀察屬性,包括簡單屬性,一對一關(guān)系和一對多關(guān)系。一對多關(guān)系的觀察者被告知所做出的變化類型以及變化中涉及的對象。
A simple example illustrates how KVO can be useful in your application. Suppose a
Personobject interacts with anAccountobject, representing the person’s savings account at a bank. An instance ofPersonmay need to be aware of when certain aspects of theAccountinstance change, such as the balance, or the interest rate.
一個簡單的例子說明了KVO在你的應(yīng)用程序中是如何發(fā)揮作用的。假設(shè)Person對象和Account對象交互,表示一個人在銀行的儲蓄賬戶。Person的實例可能需要知道Account實例某些方面何時發(fā)生變化,比如余額或者利率。

If these attributes are public properties of
Account, thePersoncould periodically poll theAccountto discover changes, but this is of course inefficient, and often impractical. A better approach is to use KVO, which is akin toPersonreceiving an interrupt when a change occurs.
如果這些屬性時Account的公開屬性,Person可以定期輪詢Account來發(fā)現(xiàn)變化,但是這樣是低效的,而且通常不切實際。一個更好的方法時使用KVO,類似于當(dāng)變化發(fā)生時Person接收中斷。
To use KVO, first you must ensure that the observed object, the
Accountin this case, is KVO compliant. Typically, if your objects inherit fromNSObjectand you create properties in the usual way, your objects and their properties will automatically be KVO Compliant. It is also possible to implement compliance manually. KVO Compliance describes the difference between automatic and manual key-value observing, and how to implement both.
要使用KVO,首先必須確定被觀察的對象(這個例子中的Account)符合KVO。通常,如果你的對象繼承自NSObject而且使用常規(guī)方式創(chuàng)建屬性,你的對象和他們的屬性將自動遵從KVO,也可以手動實現(xiàn)。 KVO Compliance介紹了自動和手動KVO的不同以及如何實現(xiàn)它們。
Next, you must register your observer instance, the
Person, with the observed instance, theAccount.Personsends anaddObserver:forKeyPath:options:context:message to theAccount, once for each observed key path, naming itself as the observer.
接下來,必須通過被觀察的實例(Account)注冊觀察者實例(Person)。Person給Account發(fā)送addObserver:forKeyPath:options:context:消息,每個被觀察的鍵發(fā)送一次,將它自己命名為觀察者。

In order to receive change notifications from the
Account,Personimplements theobserveValueForKeyPath:ofObject:change:context:method, required of all observers. TheAccountwill send this message to thePersonany time one of the registered key paths changes. ThePersoncan then take appropriate action based upon the change notification.
為了從Account接受變化的通知,Person實現(xiàn)了所有觀察者必需實現(xiàn)的方法observeValueForKeyPath:ofObject:change:context: 。只要其中一個注冊的鍵路徑發(fā)生變化,Account就會把這個消息發(fā)送給Person。然后,Person可以根據(jù)變化通知采取適當(dāng)?shù)牟僮鳌?br>

Finally, when it no longer wants notifications, and at the very least before it is deallocated, the
Personinstance must de-register by sending the messageremoveObserver:forKeyPath:to theAccount.
最后,當(dāng)不想要接收通知時,并且至少在它被銷毀之前,Person實例必須通過向Account發(fā)送消息removeObserver:forKeyPath:來取消注冊。

個人總結(jié):
1.KVO是一種觀察者機(jī)制,允許一個對象觀察其他對象的屬性的變化。
2.使用KVO整體分為四步:
- 確定被觀察者對象允許使用KVO(對象繼承自NSObject并且通過setter或KVC對屬性賦值)
- 注冊觀察者對象
- 觀察者實現(xiàn)回調(diào)方法
- 觀察者移除觀察