Database Reference
In-Depth Information
NSLog(@ "Attribute %@ changed from %@ to %@" , keyPath,
[change valueForKey:NSKeyValueChangeOldKey],
[change valueForKey:NSKeyValueChangeNewKey]);
}
When the variable is changed, we see output similar to the following:
Attribute name changed from untitled to Beef Chili
When we are done observing a value, we can stop receiving messages by
passing -removeObserver:forKeyPath: to the observed object.
id myRecipe = ...
[myRecipe removeObserver:self
forKeyPath:@ "name" ];
KVO allows views to automatically refresh themselves from the model when
the data has changed. When a view is initialized, it uses KVO to connect all
its components to the underlying objects and uses the notifications to refresh
itself.
8.3
Cocoa Bindings and Core Data
The combination of KVO/KVC (collectively referred to as Cocoa Bindings) and
Core Data reduces the amount of code that we are required to write by a
considerable amount. In the previous chapter, we wrote almost no code to
create and display our recipe objects. Nearly all the work that we did was in
Interface Builder. In this section, we discuss each of the interface objects that
we used and how they work with Core Data.
How does this apply to our application? Let's review the user interface that
we built in Chapter 7, Adding a Desktop Foundation , on page 121 and how we
used KVO and KVC.
NSTableView
Our recipe application makes heavy use of the NSTableView . In the main window
of our application, we have two table views: one to list all of the recipes and
another to list the ingredients for those recipes. Whenever an application
needs to display a list of items or a grid of data, the NSTableView is the element
to use.
In an NSTableView , like in the NSOutlineView , we do not actually bind the table
itself to the NSArrayController . Instead, we select each column individually and
bind it to a property of the objects in the NSArrayController (see Figure 27, Select
each NSTableColumn individually , on page 144 ). As we did in Chapter 7, Adding
a Desktop Foundation , on page 121 , we bind the column to the arrangedObjects
 
 
Search WWH ::




Custom Search