KVO技术指南 ----ios官方

所需积分/C币:10 2014-11-10 15:48:30 465KB PDF
3
收藏 收藏
举报

KVO技术指南 ----ios官方
Listings Registering for Key-Value Observing 7 Listing 1 Registering the inspector as an observer of the opening Balance property 7 Listing 2 Implementation of observe Value ForKey Path: ofObject: change: context: 9 Listing 3 Removing the inspector as an observer of opening Balance 10 KVO Compliance 11 Listing 1 Examples of method calls that cause Kvo change notifications to be emitted 11 Listing 2 Example implementation of automatically NotifiesObservers ForKey: 12 Listing 3 Example accessor method implementing manual notification 13 Listing 4 Testing the value for change before providing notification 13 Listing 5 Nesting change notifications for multiple keys 13 Listing 6 Implementation of manual observer notification in a to-many relationship 14 Registering Dependent Keys 15 Listing 1 Example implementation of key Paths ForValuesAffectingValueForKey: 15 Listing 2 Example implementation of the key paths ForvaluesAffecting<Key> naming convention 16 2012-07-17 Copyright o 2003, 2012 Apple Inc. All Rights Reserved 3 Introduction to Key-Value Observing Programming Guide Key-value observing is a mechanism that allows objects to be notified of changes to specified properties of other objects Important: In order to understand key-value observing, you must first understand key-value coding At a glance 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). 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 There are three steps to setting up an observer of a property. Understanding these three steps provides a clear illustration of how kyo works First, see whether you have a scenario where key-value observing could be beneficial, for example, an object that needs to be notified when any changes are made to a specific property in another object Bankobject Personobiect @property int accountBalance For example, a Personobject will want to be aware of any changes made to their accountBa lance in the Bankobject. 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 4 Introduction to Key-Value Observing Programming Guide At a glar 2. The personobject must register as an observer of the bankobject's account ba lan ce property by sending an addObserver: forkey Path: options: context: message Connection established Bankobject Personobject LbankInstance addobserver: personInstance forkeypath: @"accountBalance options: NSKeyVaLueObservingOptionNew context: NULL] Note: The addobserver: forkey Path: options: context: method establishes a connection between the instances of the objects that you specify. A connection is not established between the two classes, but rather between the two specified instances of the objects 3. In order to respond to change notifications, the observer must implement the observevalue ForKey Path: ofobject: change: context: method. This method implementation defines how the observer responds to change notifications. It is in this method that you can customize your response to a change in one of the observed properties. Connection established Bankobject Person Object (void) observe ValueForkeypath: (NSString *)keyPath ofobject: (idobject change: NSDictionary *)change context: (void *context { /custom implementation //be sure to call the superclass' implementation //if the superclass imp lements it Registering for Key-Value Observing (page 7)describes how to register and receive observation notificatⅰons 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 5 Introduction to Key-Value Observing Programming Guide At a glance 4. The observevalue Forkey Path: ofObject: change: context: method is automatically invoked when the value of an observed property is changed in a kvo-compliant manner, or if a key upon which it depends is changed [bankInstance setAccount Balance: 50]; Automatically notifies person Bankobject Personobject Person can react accordingly customized method Registering Dependent Keys (page 15)explains how to specify that the value of a key is dependent on he value of another key KVOs primary benefit is that you dont have to implement your own scheme to send notifications every time a property changes Its well-defined infrastructure has framework-level support that makes it easy to adopt-typically you do not have to add any code to your project In addition the infrastructure is already full-featured, which makes it easy to support multiple observers for a single property, as well as dependent values KVO Compliance(page 1 1)describes the difference between automatic and manual key-value observing and how to implement both Unlike notifications that use NSNotification Center, there is no central object that provides change notification for all observers. Instead, notifications are sent directly to the observing objects when changes are made. NSObject provides this base implementation of key-value observing, and you should rarely need to override these methods Key-Value Observing Implementation Details (page 19)describes how key-value observing is implemented 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 6 Registering for Key-Value Observing In order to receive key-value observing notifications for a property three things are required The observed class must be key-value observing compliant for the property that you wish to observe You must register the observing object with the observed object, using the method addobserver: forKey Path: options: context: The observing class must implement observevalueForKey Path: ofobject: change: context Important: Not all classes are Kvo-compliant for all properties. You can ensure your own classes are KVO-compliant by following the steps described in KVO Compliance(page 11). Typically properties in Apple-supplied frameworks are only kvo-compliant if they are documented as such Registering as an observer In order to be notified of changes to a property, an observing object must first register with the object to be observed by sending it an addobserver: forKey Path: options: context: message, passing the observer object and the key path of the property to be observed the options parameter specifies the information that is provided to the observer when a change notification is sent Using the option NSKey Va lueobservingoptionold specifies that the original object value is provided to the observer as an entry in the change dictionary. Specifying the NSKeyValueobservingOptionNew option provides the new value as an entry in the change dictionary. To receive both values, you would bitwise oR the option constants The example in Listing 1 demonstrates registering an inspector object for the property opening Balance Listing 1 Registering the inspector as an observer of the opening Balance property (void)registerAsobserver i Register inspector to receive change notifications for the opening Balance property of the account object and specify that both the old and new values of opening Ba lance should be provided in the observe.method 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved Registering for Key-Value Observing Receiving Notification of a Change */ [account addobserver: inspector forKey Path: @opening Ba lance options: (NSKeyValueobservingOptionNew NSKeyValueObservingOptionold) context: NULL]; When you register an object as an observer, you can also provide a context pointer. the context pointer is provided to the observer when observevalue forkey path: ofobject: change: context: is invoked The context pointer can be a C pointer or an object reference the context pointer can be used as a unique identifier to determine the change that is being observed, or to provide some other data to the observer Note: The key-value observing addObserver: forKey Path: options: context: method does not maintain strong references to the observing object, the observed objects, or the context. You should ensure that you maintain strong references to the observing, and observed, objects, and the context as necessary Receiving Notification of a Change When the value of an observed property of an object changes the observer receives an observevalue forkeyPath: ofobject: change: context: message. all observers must implement this method The observer is provided the object and key path that triggered the observer notification, a dictionary containing details about the change, and the context pointer that was provided when the observer was registered The change dictionary entry NSKey Va lue ChangeKindKey provides information about the type of change NSKeyvalueChangesetting. Depending on the options specified when the observer was registered, the that occurred. If the value of the observed object has changed, the NsKey va lue ChangekindKey entry returns NSKeyValueChangeoldKey and NSKey Va lue ChangeNewKey entries in the change dictionary contain the values of the property before, and after, the change. If the property is an object, the value is provided directly If the property is a scalar or a C structure, the value is wrapped in an NSValue object (as with key-value coding 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 8 Registering for Key-Value Observing Receiving Notification of a Change If the observed property is a to- many relationship the NSKeyVa lue ChangeKindKey entry also indicates whether objects in the relationship were inserted, removed, or replaced by returning NSKeyvalueChangeInsertion, NSKey Va lue ChangeRemoval, or NSKey va lue ChangeRep lacement, resp ly The change dictionary entry for NSKey Va lue ChangeIndexesKey is an NSIndexSet object specifying the indexes in the relationship that changed. If NSKeyvalueObservingOptionNew or NSKey Va lueObservingOptionold are specified as options when the observer is registered, the NSKey Value Changeo ldKey and NSKeyValueChangeNewkey entries in the change dictionary are arrays containing the values of the related objects before, and after, the change The example in Listing 2 shows the observevalue Forkey Path: ofobject: change: context implementation for an inspector that reflects the old and new values of the property openingBalance, as registered in Listing 1 (page 7) Listing 2 Implementation of observe value ForKey Path: ofobject: change: context (void)observevalue forkey path: (NSString *key Path ofobject: (id)object change:(NSDictionary *)change context:(void *)context i if [key Path isequal: openingBa lance"])t [openingBalanceInspectorField setobjectValue change object ForKey: NSKeyvalue ChangeNewKey ]]: 木 Be sure to call the superclass s imp lementation if it imp lements it* NSobject does not imp lement the method */ [super observevalue ForKeypath: key Path ofobject: object change: change context: context]: 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 9 Registering for Key-Value Observing Removing an object as an observer Removing an object as an observer You remove a key-value observer by sending the observed object a removeobserver: forKeyPath: message, specifying the observing object and the key path. The example in Listing 3 removes the inspector as an observer of openingBa lance Listing 3 Removing the inspector as an observer of opening Balance (void)unregisterForChangeNotification i [observedobject removeobserver: inspector forKey Path: @openingBalance] If the context is an object, you must keep a strong reference to it until removing the observer. After receiving a removeObserver: forkeyPath: message, the observing object will no longer receive any observevalue ForKeyPath: ofobject: change: context: messages for the specified key path and object 2012-07-17 Copyright O 2003, 2012 Apple InC. All Rights Reserved 0

...展开详情
试读 21P KVO技术指南 ----ios官方
立即下载 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
关注 私信
上传资源赚钱or赚积分
最新推荐
KVO技术指南 ----ios官方 10积分/C币 立即下载
1/21
KVO技术指南 ----ios官方第1页
KVO技术指南 ----ios官方第2页
KVO技术指南 ----ios官方第3页
KVO技术指南 ----ios官方第4页
KVO技术指南 ----ios官方第5页

试读结束, 可继续读2页

10积分/C币 立即下载