PDA

View Full Version : NSPopUpButton vs. Cocoa Bindings Error




HiRez
Mar 9, 2006, 04:26 PM
Hi, I'm getting a strange error trying to bind objects into an NSPopUpButton. In Interface Builder, I have an NSArrayController bound an array in my document. I'm binding the contentObjects of my popup to the array controller, and the contentValues to a string key within the objects in the array. It all works well except for one thing:

42818

The two top objects are the ones in the array, and they're correct. The goofy one on the bottom is I guess being inserted by the runtime initially, and I have no idea why. RBAudioDevice is the class of the objects in the bound content array. Now, when I select one of the other two normal items, the strange one goes away and never returns. After that, it works perfectly, but I can't get it to stop that one item from appearing when the app launches. "Automatically prepares content" does not make a difference, nor do any of the other settings. Any ideas? I've never seen this before.



caveman_uk
Mar 10, 2006, 03:02 AM
I have used bindings for this before and IIRC you connect contentObjects to the array controller as you've done and use the key field to select the appropriate key in the object. I don't think you need to connect contentValues as well. The currently selected object is specified using one of the 'selected...' bindings. I usually use selectedIndex.

This is all from memory so I could be mistaken...

HiRez
Mar 10, 2006, 04:36 AM
I don't think you need to connect contentValues as well. I think I had to do that because if you don't, it invokes description: on your object for the text it places in the popup (the item titles) and I didn't want that behavior (I needed a certain other string). Anyway, I may have figured it out. I think it has something to do with a situation where the KVO system gets to the popup before your code can populate it (it queries the array before it's initialized), and it inserts that dummy object. It's hard to tell since I have no way of knowing what the bindings runtime system is doing behind the back of my code.

caveman_uk
Mar 10, 2006, 05:21 AM
I do have to admit whilst I think the bindings systems is very useful, it isn't the panacea some people like to make out. It has a whole set of its own gotchas and if you want to do something a bit special (such as custom cells in tableviews) you're back writing datasource methods.