In Interface Builder, Inspector > Bindings, you can handily reach outside your object and bind to, for example, Application or User Defaults. But in code the 'keyPath' is always relative to the current object.

In the following example, I have a view bound to a document's 'foo'. Although 'foo' is nominally the local attribute 'bar', 'bar' can say to use the application default. So, 'foo' is affected by two key paths, a "normal" or "relative" key path to 'bar', and an "absolute" key path beginning in the app delegate. So I added a little accessor to reach the appDelegate, relatively.

To my amazement, this kludge actually seems to work -- If self.bar is USE_APP_DEFAULT, a view bound to foo changes (albeit sometimes after a click or two) when appDelegate's defaultBar property changes. Is there a more standard way to achieve this?

- (NSNumber*)foo {
    NSNumber* foo = [self bar] ;
    if ([foo intValue] == USE_APP_DEFAULT) {
        foo = [[NSApp delegate] defaultBar] ;
    }

    return foo ;
}

- (NSString*)appDelegate {
    return [NSApp delegate] ;
}

+ (NSSet*)keyPathsForValuesAffectingFoo {
    return [NSSet setWithObjects:
            @"bar",
            @"appDelegate.defaultBar",
            nil] ;
}


Jerry Krinock

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to