Cocoa Bindings

Wil Shipley of Delicious Monster pointed out that the challenge from Chapter 4 of Aaron Hillegas’ book “Cocoa Programming for Mac OS X” can be easily solved by using Cocoa Bindings, rather than using the more typical target/action approach. When I was going through Aaron’s book, I hadn’t been exposed to Cocoa Bindings yet, since those aren’t introduced until Chapter 6.

I thought, ‘Huh, yep, that way looks pretty easy,’ but there are a few steps missing from Wil’s description that are critical if it’s to work right. I admit I don’t understand all of them; I’m busy reading the Cocoa Bindings and Core Data documentation to try to fill in my understanding.

So here’s what I did:
Add a controller class for the methods. This is implied and easy to see. I called mine AppController.

Remove the button from the Window in IB; you don’t need it anymore!

Drag an NSObjectController instance from the palette to your NIB. Change the class name to match the controller you created in step 1.

Now, your object controller should show up in the Bindings -> Value -> Bind To inspector when you’ve got either your input or your output text field selected. Without adding the NSObjectController, I floundered around for a minute or two trying to figure out what to do at this stage of trying to follow Wil’s example.

For some reason I had to modify Bindings -> Availability -> Editable and Enabled for the input text field; I set them both to isEditable on NSObjectController. Finally, I selected the NSObjectController in my NIB and set its Editable attribute to True. Now when I run the app, I can enter characters in the input field, tab or return out, and the count of characters updates to the output field. Pretty cool!

I think someone should sponsor a contest to see who can make the most useful application with no code.

This entry was posted in Uncategorized and tagged . Bookmark the permalink.