views:

599

answers:

2

Is it possible using NSArrayController to bind a NSTextField's value to a particular item in the array? In particular, I want to bind to a property on the first item in the array, and show nothing if the array is empty.

Using arrangedObjects.command shows just "(" -- presumably it's trying to show a multi-line string with comma-separated strings for each item. I just want the first one.

+5  A: 

Bind the text field to selection.command, and programmatically set the array controller's selection index to 0. You may need to also re-set the selection index to 0 any time you add or remove items to the content array.

Obviously, this won't work if you're allowing the user to select items within the array controller (you'd need a second array controller). I'm assuming that's not the case, since if it were, I'd expect you to want to show the user-selected object, instead of always the first object.

Peter Hosey
I hadn't thought of the selection property. Thank you -- that should work. (And you're right, this array is never user-editable).
Daniel Dickison
A: 

I have a similar question about using an NSArrayController. Once I set it up as Peter described, it successfully displays just the object at whatever index I want, but it does two things wrong. First, a table view displays the same (correct) object one time for each member of the content array. Secondly, I can't find a way to prevent the array controller from being selectable. Any help?

Ben Fenker
Present Me is looking at Past Me's answer and shaking his head. The correct solution is to do nothing like this—if the object that is first in the array has some special status, you and the original questioner should make a separate non-array property (in the same object that holds the original array, from which I assume the array controller is getting it) to hold the object that has that status.
Peter Hosey
Also, you should post this as a separate question, not as an answer on this question.
Peter Hosey
Thanks Peter, I've asked a question here:http://stackoverflow.com/questions/2089929/cocoa-binding-to-single-object-from-an-array
Ben Fenker