Skip navigation.
 
mlRe: Why aren't my bindings firing?
FROM : Ron Lue-Sang
DATE : Mon Jun 30 23:43:38 2008

Sorry, I haven't read every message in this thread, but I think I can 
answer the original question

> [[self bar] bind:@"title" toObject:ivar_controller 
> withKeyPath:@"selection.displayName" options:nil];
>
> Now here's the thing: if I call setDisplayName: on Foo, it calls 
> Bar's setTitle: method, exactly as it should. However, if I call 
> setTitle: on Bar, it does *not* end up calling Foo's setDisplayName: 
> method, although it seems like it should. I can change the 
> bind:toObject:withKeyPath:options: invocation above so that it binds 
> Bar directly to Foo without going through the object controller, or 
> I can try going the other way and binding the Foo to the Bar - 
> always I get the same result.



Bindings of this type are one way. Their kinda ad hoc. "Title" will be 
updated to match ivar_controller.selection.displayName. Changing title 
will not result in any KVC calls to change anything in the 
ivar_controller. This is a read-only setup.

It sounds like you're asking for 2 things.
1) To have logic that will update ivarController.selection.displayName 
to match "title".
2) To have the logic from #1 triggered when setTitle is called.

There was some suggestion about using infoForBinding: to do blah blah 
blah.

Yes! infoForBinding is what you should use to implement the logic in #1.

// in Bar get the infoForBinding dictionary
NSDictionary *info = [self infoForBinding:@"title"];

// extract the controller and keypath info
id controller = [info objectForKey:NSObservedObjectKey];
NSString *keyPath = [info objectForKey:NSObservedKeyPathKey];

// extra points for looking in the binding options for value 
transformers and stuff
// options = [info  objectForKey:NSOptionsKey];

// push/write the value to the controller
[controller setValue:theTitle forKeyPath:keyPath];

The trick to getting #2 is that setTitle is ALSO what gets called 
during the regular we-did-the-read-work-for-you bindings update from 
the controller. Don't let changing the displayName end up calling 
setTitle if setTitle's going to set the displayName. Make sense?


So how do we fix this issue?

2a) Don't use setTitle to trigger logic in #1. This is the approach I 
normally suggest. Your views should push these changes down due to 
user interaction, not programatic fiddling.

2b) Implement the "read" logic yourself by implementing bind:. This 
means you'll need to cache the binding info yourself, start observing 
the controller object, and react to the KVO notifications 
appropriately. Don't call super for bind:/unbind:/infoForBinding: 
@"title".


As a final note - I'm not sure if anyone else mentioned it. The 
original code snippets had stuff like

- (void)setTitle:(NSString *)title {
    [self willChangeValueForKey:@"title"];

    NSLog(@"setting title to %@", title);

    if(title != ivar_title) {
        [ivar_title release];
        ivar_title = [title copy];
    }

    [self didChangeValueForKey:@"title"];
}


You don't need the will/did changeValueForKey:@"title" calls in the 
setTitle: method if you haven't disabled automatic KVO notifications 
for title. If you didn't know you could do that, then you haven't 
disabled KVO notifications for your class and, therefore, don't need 
to call will/did changeValueForKey:@"title".

Hope that clears things up.


On Jun 30, 2008, at 2:04 PM, Hamish Allan wrote:

> On Mon, Jun 30, 2008 at 3:56 PM, Michael Ash <michael.<email_removed>> 
> wrote:
>

>> Although Apple's sample code shows overriding -bind:... to store
>> information about the new binding, it doesn't look like this is
>> necessary. You can simply use -infoForBinding: to obtain the info
>> dictionary, extract the bound object and key, and use that 
>> information
>> to update the model object. I'd assume this is what the Apple classes
>> do, and it seems to me to be a lot simpler than overriding -bind:...
>> to stash away a bunch of information that's already being stored for
>> you anyway.

>
> That would seem a pretty reasonable assumption, but it doesn't seem to
> be the case. At least, breakpoints on -[NSTextField infoForBinding:],
> -[NSControl infoForBinding:],  -[NSObject infoForBinding:] don't seem
> to trigger when the text field is edited; nor are messages logged from
> a subclass of NSTextField overriding -infoForBinding:.
>
> Any other ideas, anyone?
>
> Thanks,
> Hamish
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>



--------------------------
RONZILLA

Related mailsAuthorDate
mlWhy aren't my bindings firing? Charles Srstka Jun 27, 21:37
mlRe: Why aren't my bindings firing? Keary Suska Jun 28, 00:17
mlRe: Why aren't my bindings firing? Charles Srstka Jun 28, 00:53
mlRe: Why aren't my bindings firing? Ken Thomases Jun 28, 03:06
mlRe: Why aren't my bindings firing? Charles Srstka Jun 28, 06:08
mlRe: Why aren't my bindings firing? Ken Thomases Jun 28, 08:00
mlRe: Why aren't my bindings firing? Charles Srstka Jun 28, 08:30
mlRe: Why aren't my bindings firing? Ken Thomases Jun 28, 08:57
mlRe: Why aren't my bindings firing? Charles Srstka Jun 28, 10:10
mlRe: Why aren't my bindings firing? Ken Thomases Jun 28, 11:55
mlRe: Why aren't my bindings firing? Steve Weller Jun 28, 16:26
mlRe: Why aren't my bindings firing? Charles Srstka Jun 28, 17:17
mlRe: Why aren't my bindings firing? Charles Srstka Jun 28, 19:31
mlRe: Why aren't my bindings firing? Ken Thomases Jun 29, 08:59
mlRe: Why aren't my bindings firing? Hamish Allan Jun 30, 06:30
mlRe: Why aren't my bindings firing? Charles Srstka Jun 30, 06:33
mlRe: Why aren't my bindings firing? Hamish Allan Jun 30, 06:38
mlRe: Why aren't my bindings firing? Michael Ash Jun 30, 16:56
mlRe: Why aren't my bindings firing? Hamish Allan Jun 30, 23:04
mlRe: Why aren't my bindings firing? Ron Lue-Sang Jun 30, 23:43
mlRe: Why aren't my bindings firing? Hamish Allan Jun 30, 23:53
mlRe: Why aren't my bindings firing? Michael Ash Jul 1, 01:56
mlRe: Why aren't my bindings firing? Ron Lue-Sang Jul 1, 01:58
mlRe: Why aren't my bindings firing? Hamish Allan Jul 1, 21:45
mlRe: Why aren't my bindings firing? Ken Thomases Jul 2, 11:14
mlRe: Why aren't my bindings firing? Scott Anguish Jul 2, 18:31
mlRe: Why aren't my bindings firing? Hamish Allan Jul 3, 01:04
mlRe: Why aren't my bindings firing? mmalc crawford Jul 3, 02:12
mlRe: Why aren't my bindings firing? Scott Anguish Jul 3, 02:13
mlRe: Why aren't my bindings firing? Hamish Allan Jul 8, 00:51
mlRe: Why aren't my bindings firing? Chris Hanson Jul 8, 04:39
mlRe: Why aren't my bindings firing? Ron Lue-Sang Jul 8, 06:29
mlRe: Why aren't my bindings firing? Scott Anguish Jul 8, 12:11
mlRe: Why aren't my bindings firing? Hamish Allan Jul 8, 17:33
mlRe: Why aren't my bindings firing? Scott Anguish Jul 9, 03:43
mlRe: Why aren't my bindings firing? Hamish Allan Jul 9, 06:39
mlRe: Why aren't my bindings firing? mmalc crawford Jul 9, 07:36
mlRe: Why aren't my bindings firing? Scott Anguish Jul 9, 23:39
mlRe: Why aren't my bindings firing? Hamish Allan Jul 13, 23:06
ml[Moderator] Re: Why aren't my bindings firing? Scott Anguish Jul 14, 02:27