Skip navigation.
 
mlRe: Why aren't my bindings firing?
FROM : Charles Srstka
DATE : Sat Jun 28 19:31:56 2008

On Jun 28, 2008, at 4:55 AM, Ken Thomases wrote:

> Yeah, it surprises me, too, that NSObject has an implementation of 
> bind:toObject:withKeyPath:options:.  I had thought that it was just 
> part of an informal protocol -- that is, that it was declared in a 
> category on NSObject, but never implemented except by specific 
> classes which adopted that protocol.  However, I did a symbol dump 
> of the AppKit framework and I see that there is, in fact, an 
> implementation.  I have no idea what it could be doing.


Actually, I gave it a little thought, and perhaps the purpose of 
NSObject's default implementation is just to make it easier to 
implement the stuff that's mentioned in that "how bindings work" 
document. The built-in implementation provides the bindings in one 
direction for us - all we have to do is supply the bindings in the 
other direction, so adding an NSMutableDictionary instance variable to 
the view and then adding these methods:

- (void)bind:(NSString *)key toObject:(id)obj withKeyPath:(NSString 
*)keyPath options:(NSDictionary *)options {
    NSDictionary *binding = [NSDictionary 
dictionaryWithObjectsAndKeys:key, @"key", obj, @"target", keyPath, 
@"keyPath", nil];

    [ivar_bindingsDict setObject:binding forKey:key];

    [super bind:key toObject:obj withKeyPath:keyPath options:options];
}

- (void)unbind:(NSString *)key {
    [ivar_bindingsDict removeObjectForKey:key];
}

- (void)fireNotificationsForKey:(NSString *)key {
    NSDictionary *binding = [ivar_bindingsDict objectForKey:key];

    if(binding) {
        id target = [binding objectForKey:@"target"];
        NSString *keyPath = [binding objectForKey:@"keyPath"];

        [target setValue:[self valueForKey:key] forKeyPath:keyPath];
    }
}

and then just calling [self fireNotificationsForKey:@"someKey"] each 
time you modify whatever it is in the view's state that will cause the 
binding "someKey" to need to be updated, would pretty much do it, am I 
right?

(I suppose you'd also want to expose your available bindings by 
calling +exposeBinding: in your +initialize method and implement 
valueClassForBinding: if you're going to make an Interface Builder 
palette for your view)

Charles

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