NSKey informal protocol provides methods to create and remove bindings between view objects and controllers or controllers and model objects. In addition, it provides a means for a view subclass to advertise the bindings that it exposes. This informal protocol is implemented by
NSObject and its methods can be overridden by view and controller subclasses.
- macOS 10.3+
When a new binding is created it relates the receiver’s binding (for example, a property of the view object) to a property of the observable object specified by a key path. When the value of the specified property of the observable object changes, the receiver is notified using the key-value observing mechanism. A binding also specifies binding options that can further customize how the observing and the observed objects interact.
Bindings are considered to be a property of the object which is bound, and all information related to bindings should be owned by the object. All standard bindings on AppKit objects (views, cells, table columns, controllers) unbind their bindings automatically when they are deallocated, but if you create key-value bindings for other kind of objects, you need to make sure that you remove those bindings before deallocation (observed objects have weak references to their observers, so controllers/model objects might continue referencing and messaging the objects that were bound to them).
Bindings between objects are typically established in Interface Builder using the Bindings inspector. However, there are times it must be done programmatically, such as when establishing a binding between objects in different nib files.
NSView subclasses can expose additional key-value-coding/key-value-observing compliant properties as bindings by calling the class method
expose for each of the properties. This is typically done in the class’s
initialize method. By exposing the bindings that an object supports and creating an Interface Builder palette, you can make instances of your own classes bindable in Interface Builder.