NSMatrix and Bindings


Update: I accidentally deleted most of my source downloads, and this was one I didn’t have a stored copy of. If you have one, please email it to me at this domain at mac dot com. In the meantime, the downloads linked from this post will get a 404.

While I was reading Wil Shipley’s latest post about NSOutlineView, I noticed that one of the comments had a posting about trouble binding to an NSMatrix. That posting is here.

The trouble is, jwz wants to bind several radio buttons to a preferences (user defaults) value. And he wanted the user defaults string to be different than the title strings on the radio buttons. Given that, he was logically trying to bind an array of values for the prefs, and array of values for the titles. Unfortunately, that’s not what NSMatrix expects. (He was also doing it purely in code whereas in InterfaceBuilder, you can see how binding a given value enables or disables other bindings.) NSMatrix expects you to bind an object to each button. If you want, you can stop there. It will call -description on each object to get a title/label for each button. Or, you can bind “contentValues” to a key within each object to provide the title. That last sentence is the important part: You can’t bind each button to just an NSString - because then NSMatrix uses the string both as the value of the button itself and as the title.

I wrote up a brief example - you can download it here.

In my example, I bound each radio button to an NSDictionary. Within that dictionary, I provided a “title” key and bind the radio button labels to that value. That was the easy part. The trouble comes when I tried to use a simple string value - different than the value I used for the button label - to store in the NSUserDefaults. I don’t think there is an easy way to do that. So the route I took was to store the whole dictionary object, and I added a key for “prefsValue” - so if jwz wants reference a simple string, he can pull that out of the prefs and do his thing. I could have also just bound the button label / title. Either way, I couldn’t find a way to both bind a simple string and have that string be different than the label. Although, NSMatrix does provide two other bindings options for the selected radio button - by tag (an integer that you can define for each button) and by index. Hopefully one of these four bindings will get jwz what he wants.

But, the big thing to note is that you can’t use two disparate strings for your NSMatrix bindings - it expects each button to represent an object, and optionally each title/label to be the value of a key within that object.

UPDATE: Argh! Something in the back of my head was bugging me about this, so I just tried adding one more binding… and it works: it’s possible to get exactly the behavior that jwz is looking for. Here’s what I did:

  • I loaded up an NSArrayController with NSDictionary objects. Each dictionary contained two key-value pairs. (same as above)
  • I bound the content of the NSMatrix to the NSArrayController’s arrangedObjects key. (same as above)
  • To use a specify value as the title of each radio button, I bound the contentValue item to arrangedObjects.title (same as bove).
  • To get the store a user default that’s just a simple string, I bound the NSMatrix contentObject to the other dictionary key - arrangedObjects.prefsValue. (this is what I added compared to what I described above).
  • I bounded the selectedObject key to a NSUserDefaults value called textMode so that the selected radio button’s contentObject saved. (same as above).

So, to recap, the key is understanding that NSMatrix expects a hierarchy of bindings - first bind the content key to the array of objects, then optionally specify keys within those objects to serve as the titles/labels and the selection values.

I updated the code and reposted it to the link above. Enjoy.

2 Responses to “NSMatrix and Bindings”


  1. 1 Jerrod Aug 9th, 2006 at 12:38 pm

    Your link for the “http://www.blakeseely.com/source/NSMatrixBindings.zip” is broken. Could you send me a copy of that? thanks. -Jerrod

  2. 2 blake Aug 9th, 2006 at 2:08 pm

    Hey Jerrod - I’m aware of this - check out the text at the very top of the article…

Leave a Reply