Chapter 11. Cocoa Bindings

So far in this book, we’ve talked at length about how the model-view-controller paradigm works in Cocoa and Cocoa Touch, and how dividing up your application’s code into these separate areas of responsibility leads to easier-to-manage codebases.

However, sometimes it may seem like overkill to write separate models, views, and controllers, especially when all the controller needs to do is pass information directly from the model to the view and vice versa. In many cases, the only behavior you want is for a label to display information stored in a model object.

To solve this problem, Apple introduced bindings in OS X. Bindings are connections between views and objects, where the contents of the object are used to directly drive what the view displays. Bindings mean you can write less code for the same excellent features.

In this chapter, you’ll learn how to use bindings to connect your interface directly to data. You’ll also learn how to use the built-in controller classes that Apple provides to manage collections of objects. By the end of the chapter, you’ll have created a sophisticated note-taking application while writing minimal code.

Note

Bindings are only available on OS X. Sorry, iOS developers!

Binding Views to Models

A binding is simply an instruction that you give to a view about where its content comes from. The instruction is along the lines of, OK, text field, the text that you should show comes from this object over here. If it ever changes, update ...

Get Swift Development with Cocoa now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.