Cocoa Bindings on macOS

Cocoa bindings make glue code a thing of the past. Discover how you can simplify your controller code in this Cocoa Bindings on macOS Tutorial! By Andy Pereira.

Leave a rating/review
Save for later
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Adding a Little More Detail

Cocoa Bindings can do more than what you’ve learned so far: you can bind colors and fonts to labels, enable and disable controls, and even set different values for labels depending on their state.

Build and run your app, and you’ll notice that you see No Selection on all your labels before. This isn’t very nice for a user to see when they start your app.

Find the label titled Price Label (Bind), and expand Value. Under No Selection Placeholder, put -- as shown below:

Screen Shot 2016-08-30 at 12.37.50 PM

Build and run your app, and you’ll see that the Price label now has a nice placeholder value:

Screen Shot 2016-08-30 at 12.38.00 PM

Set all labels that say No Selection to have a placeholder of your choice.

Note: If you want the label to be blank, you can set the No Selection Placeholder to a space.

Where to Go From Here?

That’s the basics of Cocoa Bindings on macOS. You’ve seen how easy it can be to connect data and UI.

In this tutorial, you learned the following:

  1. How to use Interface Builder to quickly and easily bind objects to data.
  2. How to keep models and views in sync with the user’s current selection.
  3. How to use methods and bindings together to control behaviors and organize data.
  4. How to quickly build out UI features like progress spinners.

You can download the final project here. Hopefully, you can save a lot of time (and code!) by adopting this technology.

Each binding has a lot of little settings and options, many of which you didn’t explore in this tutorial. Check out this resource on Cocoa bindings provided by Apple. It covers a lot of the details about what the options in the bindings windows do.

I hope you enjoyed this Cocoa Bindings on macOS tutorial and picked up some new techniques to use to accelerate your development process. You’ve just opened up a whole new universe!

Questions and comments are always welcomed in the discussion below.