How to get an "absolute" key path in code
How to get an "absolute" key path in code
- Subject: How to get an "absolute" key path in code
- From: Jerry Krinock <email@hidden>
- Date: Sat, 21 Feb 2009 08:10:55 -0800
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 (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