Version: 2.0

Posted: 2008-01-04

Build Requirements: Xcode 3.0, Mac OS X v10.5

Runtime Requirements: Mac OS X v10.5

View Source Code:

Download Sample (“WithAndWithoutBindings.zip”, 158.0K)
Download Sample (“WithAndWithoutBindings.dmg”, 202.2K)



Description

This sample illustrates the adoption of Cocoa Bindings to manage synchronization of values between models and views, and in particular the role of the NSController object:

(a) How you can consider NSController object as a refactoring of your own code; and

(b) How you can subclass an NSController class to customise its behaviour.

This is best seen if you compare the two projects using the File Merge application.

First, compare the MyDocument classes. Notice that most of the instance variables "disappear", as does all of the code related simply to updating the user interface in response to selection changes. Detail UI elements are bound to the selection of the array controller, so this is all handled by bindings.

Second, compare the TableViewDataSource files which contain the implementation of the drag and drop methods for the table view (and in the case of the first implementation, also the standard data source methods).

There is a subtle but profound difference between the TableViewDataSource files in the two projects. In the first case, the files contain a category of the MyDocument class. In the second, the files contain a subclass of NSArrayController. That the standard data source methods disappear should not be a surprise. What may be more unexpected, however, is how little code changes in the drag and drop methods.

Here responsibility for drag and drop has been passed to the object with most similar other responsibilities. So in terms of refactoring, you can consider the NSArrayController instance to have absorbed the functionality you would normally have to have implemented yourself (in the table view data source methods) whilst still allowing the extensibility of subclassing to provide additional custom behaviour.

Finally, notice that the WithBindings project includes a simple value transformer. It takes over responsibility from the MyDocument object in the WithoutBindings version of converting between an URL in the bookmark object and a string in the URL text field in the window. (The 'value' binding for the URL text field has "StringToURLTransformer" as its value transformer option.)

Document Revision History

Date Notes
2008-01-04Updated to use Objective-C 2.0.
2007-08-23Fixed WithBindings MyDocument nib file (previous version could not be opened on Mac OS X v10.4). Updated table view data source to use API appropriate for Mac OS X v10.4: * The table view data source now uses tableView:writeRowsWithIndexes:toPasteboard: instead of tableView:writeRows:toPasteboard: * The table view drag mask is set using setDraggingSourceOperationMask:forLocal: instead of using a subclass of NSTableView
2007-06-05N/A
2007-05-31Illustrates the adoption of Cocoa Bindings and refactoring using a subclass of NSArrayController.

Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.