Re: Binding NSTextField to an array
Re: Binding NSTextField to an array
- Subject: Re: Binding NSTextField to an array
- From: Jeremy Hughes <email@hidden>
- Date: Mon, 06 Mar 2017 17:34:03 +0000
Bingo and thanks!
I had already found the checkbox for “Selects All When Setting Content"
The missing piece of the jigsaw was using “selection" as the Controller Key and “self" as the Model Key Path.
It all works now.
Jeremy
--
> On 6 Mar 2017, at 17:12, Mike Abdullah <email@hidden> wrote:
>
> Yep, you’re very close. An array controller is required indeed, it’s the one responsible for vending out NSMultipleValuesMarker.
>
> From your earlier message, you’re now seeing an exception or similar:
> Cannot create number from object <_NSControllerObjectProxy: 0x6000000070b0> of class _NSControllerObjectProxy
>
> This is because you’ve bound the field directly to the array controller’s .selection property. .selection returns a proxy object representing the selection which can then be queried for other properties. In your case you want to bind to “selection.self” instead I believe.
>
> When binding your array controller to its content, there’s also an option to have it automatically select all (or something to that effect). You want to have this turned on too so that the array controller has a selection to generate values from.
>
> Mike.
>
>> On 6 Mar 2017, at 17:44, Jeremy Hughes <email@hidden> wrote:
>>
>>> On 6 Mar 2017, at 14:30, Jonathan Mitchell <email@hidden> wrote:
>>>
>>> Sounds like NSValueTransformer is in fact what you need.
>>> It can take in your NSArray ref and spit out a single NSString that the NSTextField binding can live with.
>>
>> I think I must be missing something obvious.
>>
>> The value of an NSTextField can be bound (in Interface Builder) to a property in File’s Owner or to an array controller that is bound to a property in File’s Owner. There is a checkbox in Interface Builder that says “Allows Editing Multiple Values Selection”.
>>
>> This suggests to me that I can bind a text field to an array or (if that's not possible) to an array controller that is bound to the array. If there are multiple values, the controller should return NSMultipleValuesMarker, which according to Apple’s documentation "indicates that more than one object is selected in the controller and the values for the requested key aren’t the same.”
>>
>> The documentation goes on to say "For example, if the value for selection.name returns an array containing three strings—”Tony”, “Tony”, “Tony”—the string “Tony” is returned instead of the NSMultipleValuesMarker.”
>>
>> What I’m trying to do is to display a textfield (with a number formatter) which should display an integer value (such as “7”) if all the values in the array are the same, or a multiple-values indicator (like “-“) if the values are not the same.
>>
>> I can get the binding to work if I bind the text field to a single value, but I can’t get it to work with an array of values or with an array controller that is bound to an array of values.
>>
>> Maybe I should give up on using bindings to do this, but it would be good to understand what I’m doing wrong.
>>
>> Jeremy
>>
>>
>> _______________________________________________
>>
>> 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
>
_______________________________________________
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