In this article, we’re going to explore Cocoa Bindings. Cocoa Bindings are a great way to give your end users more functionality and more flexibility replication while simultaneously reducing the amount of code. That’s a win-win situation for everybody.
Historically Cocoa developers have had to keep data objects and view objects in-sync manually so if you wanted use a table view you’d have to implement data source methods. Each method would have to implement the table view request and make sure that the data ended up in the right place. By just writing a few methods you would give life to very rich and very full-featured tables. Cocoa would do a lot of the hard work, but if you have multiple controls that depended upon each other there’s more work involved. For drop-down changes you have to make sure the content of the table gets updated to keep making sure that everything flows smoothly and everything is kept in sync; this could be a real pain.
Cocoa Bindings allow you to define relationships among the objects and then have Cocoa figure out what to do with those relationships at run time. This means that you have to write less code which means faster development and more functionality and you get all that for free! You can mix and match Cocoa Bindings with data source and delegate methods so if your target users have anything later than even Panther (which is Mac OS X 10.4 and everybody’s got at least Panther installed), there are very few reasons to not use Bindings. Cocoa Bindings are a collection of technologies that you can use in your applications to fully implement the model, view and controller paradigm. The models encapsulate the application data, the views display and edit that data and the controllers mediate between two. Cocoa Bindings then reduce the code dependencies among models using controllers, the Bindings support multiple ways of viewing the data, and the Bindings automatically synchronized views when the model change. The Bindings provide extensible controllers that offer protocols for models and views to adopt, and the Bindings provide additions to classes in foundation and in the application kit. You can eliminate a lot of that gory glue code that you usually have to write with controls by using the Bindings that are available in interface builder to connect your controllers with models and with views. In a very simple functional sense, the Cocoa Bindings technology provides a means of keeping your model and a few values synchronized without you having to write the glue code and allows you to establish an automatically mediated connection between the view and piece of data binding the two together in such a way that a change in one gets reflected in the other.
This Bindings technology therefore offers a way to increase the functionality and the consistency of your application while at the same time decreasing the amount of code you’ve got to write and maintain. It takes care of a lot of the aspects of user-interface maintenance for you by allowing you to offload that work of the custom glue code onto reusable prebuilt controllers.
We thank Mike Simpson, author on GogoTraining for his insight. For more information, you can contact us at http://www.gogotraining.com.