Re: Binding not setting model property
Re: Binding not setting model property
- Subject: Re: Binding not setting model property
- From: Roland King <email@hidden>
- Date: Sun, 19 Jan 2014 11:45:05 +0800
Yes I've bound to the value property in IB. That gets the value from the model object into my NSTextField perfectly via the transformer. It reads the underlying NSURL property which it's bound to, applies the transformer, and sets the text into the NSTextField (presumably by setting the stringValue property).
So perhaps the question is .. what property do I need to set on the NSTextField to get it to go back the other way? I've tried [ textField setValue:.. ] but that just throws an exception that setValue: isn't implemented on NSTextField, which it isn't, so I went back to setStringValue: But setting stringValue: changes the on-screen string, but doesn't trigger the binding to send the string back the other way.
That was one point of confusion for me right at the start. IB binds the 'value' property but as far as I could tell, NSTextField doesn't have such a property, calling 'value' or 'valueForKey:@"value"' on it just raises as exception, so I assumed bindings was doing something clever under the hood to know that the value binding for an NSTextField actually means stringValue;, or possibly it's using another method entirely which isn't called by setStringValue:.
The checkbox property I have works fine in both directions, but that's triggered by an actual mouse click which made me wonder if there's something more I have to do when setting programatically to trigger bindings to work.
On 19 Jan, 2014, at 11:21 am, Dave Fernandes <email@hidden> wrote:
> How are you setting up the binding? Normally you bind to the value property of the NSTextField (not stringValue). See Cocoa Bindings Reference (NSTextField Bindings).
>
> On Jan 18, 2014, at 9:41 PM, Roland King <email@hidden> wrote:
>
>> I have a model object with a codeURL property which is readwrite and is an NSURL.
>>
>> I have an NSTextField in the NSView subclass with the value bound to the codeURL property with a custom transform which turns a URL into a string (and deals with nil and another edge case).
>>
>> The transform is reversible, returns YES for allowsReverseTransform and has an implemented reverseTransformedValue: method.
>>
>> There's a browse button on the UI which lets the user select a file and then sets the stringValue: property of the NSTextField to the url.path of the file selected.
>>
>> What I expected is that setting the stringValue would invoke the transformer in the reverse direction and set the codeURL property to the reverse transformed value, thus binding the codeURL property in both directions.
>>
>> What actually happens is nothing. The transformer reverse method isn't called and the codeURL isn't changed. It works in the forward but not the reverse direction. I have a checkbox similarly bound (with no transform) and that works fine both ways.
>>
>> This is my first foray into bindings .. what could I be doing wrong? Do I need to express a simple NSString property on the model object and bind that instead and do the NSString/NSURL conversion there?
>>
>>
>>
>> _______________________________________________
>>
>> 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