Saturday, October 3, 2009

Turn me on, I'm a checkbox

We're building on a series of posts looking at bindings. The previous items are:

load data from the main bundle
set up the User Interface and bindings
administrative details---a data class and its header file

Next, we want construct a binding for something other than text, e.g. the state of a checkbox, which I believe is technically called an NSSwitchButton. It's very easy.

Drag a check box cell out from the Library palette and drop it on the third table column of the project, opened in Interface Builder. You don't have to check through to the table column first. Now, click on the table column to take focus away from the check box (make sure the table column is selected rather than the check box or "button" cell. Bind it to the clear key of the array controller as shown.



Now, we have to fix something. I gave a slightly incorrect version of the data plist file the other day. Open the file in the XCode editor and click on the clear button in the Value column. It should look like this:



Isn't the appearance of the plist file in XCode nice? It shows the boolean value as a checkbox! Exactly what we are doing here.

Now build and run the application. The state of the button cell is bound to the value for the "clear" key. It didn't take much work at all. If I'd only remembered to change the title of the check box cell before I took the screenshot...Oh well.