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 12:17:10 +0800
.. right so if I actually edit the NSTextField on-screen (it wasn't editable before, I made it so) *THEN* the binding gets called in the way I expected.
So it would seem that only a user-initiated change of the value fires the binding in reverse (which is what the example binding code in the Apple docs suggests) but setting the stringValue into the thing directly, doesn't. That's a surprise, I expected that either way of setting the value would result in the binding firing. I can see from the stack trace that the binding code is triggered directly from the textFieldShouldEndEditing: method.
In my case the result of an NSOpenPanel, triggered by a browse button, sets the value directly into the NSTextField, it's not the user directly editing it does it.
So unless there is a property on the NSTextField I can set which triggers its value binding to fire, or there's a method I can call on it like .. 'fireBindings' looks like I'm going to have to do what I eventually did and set the model property directly in code .. not great, but bindings got me 80% of the way which was good.
On 19 Jan, 2014, at 11:45 am, Roland King <email@hidden> wrote:
> 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
_______________________________________________
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