Re: NSSlider and arrangedObjects
Re: NSSlider and arrangedObjects
- Subject: Re: NSSlider and arrangedObjects
- From: Quincey Morris <email@hidden>
- Date: Fri, 20 Jan 2012 23:00:19 -0800
On Jan 19, 2012, at 03:03 , Tobias Wood wrote:
> I am attempting to bind an NSSlider to a property of every object in an NSArrayController, rather than just the current selection. There are other properties that are bound to the selection, but this particular one I want to change for everything, regardless. The array managed by the controller is initially empty when the program starts.
It's not entirely clear what you mean. Are you saying you want to have a single slider in your window, that somehow represents all the array elements? If so, what value would the slider show when the array elements have different values for the relevant property? Is there a table view involved in this somewhere?
From the information you've provided, the answer is probably that you can't do what you want with bindings. One possible alternative is to add a new property to your data model, that represents the "combined" property values of the array (whatever that means), and bind the slider to this property. The property getter and setter would then be responsible for co-ordinating the combined value with the individual values.
> Binding the NSSlider's value to the NSArrayController's "arrangedObjects.propertyName" causes my program to get a SIGABRT on opening, with the following uncaught exception:
> "Cannot create double from object ( ) of class __NSArray0"
>
> I assume this is to do with the empty array.
No, it's because you can't bind through a key path that contains a collection property ("arrangedObjects" in this case). This situation is complicated by the fact that NSArray responds to a selector it doesn't recognize (such as your "propertyName") by returning the result of sending this selector to every element in the array. This array result isn't convertible to the slider's numeric value by any standard KVC mechanism, so you get the exception.
> I have tried binding the NSSlider's enable to "arrangedObjects.@count" and "arrangedObjects.canRemove". Neither fixes the problem. If I bind the value to "selection.propertyName", the enabling/disabling works as expected (i.e. the slider is greyed out until the array has at least one entry). Hence I am confused.
There's an important difference between "selection" and "arrangedObjects". The latter is an array, but "selection" is a proxy object that represents either a single object from the controller (*the* selected object), or a special multiple-selection marker, or a special invalid value marker, or a special no-value marker. The special markers are used (for example) for making text fields show "multiple selection" or "no value" messages, and for automatically enabling/disabling controls as you noted.
Again, there's a complication. Table view *column* bindings are special in that they appear to be able to bind through collection objects, but in fact their key paths are treated as 2 separate parts by the bindings mechanism. The first part allows the column to find the array of values for all table rows, and to choose the correct value per row based on the row index; the second part allows the column to find the actual value to display.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden