NSDictionary and binding
NSDictionary and binding
- Subject: NSDictionary and binding
- From: Greg Hurrell <email@hidden>
- Date: Thu, 28 Oct 2004 01:46:06 +0200
I've gradually been trying to come to grips with Cocoa Bindings over
the last few months, but I have a question for which I can't find an
answer.
I have a view class (CustomView) in the preferences window of my app
which represents a complex object (ComplexObject). I'm trying to
maintain a nice separation between Model, View and Controller (MVC),
although I am still learning the MVC design pattern. I want to be able
to write the internal structure of ComplexObject (the Model) out to
NSUserDefaults, so I've written methods for getting an NSDictionary
representation of itself, and for the reverse case of initializing
itself using an NSDictionary:
@interface ComplexObject : NSObject {
// a bunch of instance variables...
}
- (id)initWithDictionary:(NSDictionary *)aDictionary;
- (NSDictionary *)dictionaryRepresentation;
@end
I can easily read/write this dictionary to/from the NSUserDefaults. Now
I'd like to set this up so that it uses Cocoa Bindings, like all of the
other preferences in the program, but I really don't know how. I know
how to bind simple things like check boxes or NSTextFields, but in this
case I am at a loss.
In the current implementation I can tell the CustomView to display the
ComplexObject by using the -value and -setValue: accessors as shown
below. I choose "value" because it seemed consistent with the
conventions, but I've also tried other names and it doesn't work.
@interface CustomView : NSView {
NSDictionary *value;
}
- (NSDictionary *)value;
- (void)setValue:(NSDictionary *)aDictionary;
@end
Inside CustomView's +initialize method I have the following, even
though I don't think it's necessary to do this:
[self exposeBinding:@"value"]; // in the run log I see this causes
-value to be messaged twice
Inside my application's main controller I have the following set to run
at launch time:
NSUserDefaultsController *prefsController =
[NSUserDefaultsController sharedUserDefaultsController];
[theCustomView bind:@"value"
toObject:prefsController
withKeyPath:@"values.ComplexObject"
options:nil];
[prefsController addObserver:self
forKeyPath:@"values.ComplexObject"
options:NULL
context:NULL];
Unfortunately, I never see
-observeValueForKeyPath:ofObject:change:context: get called when the
CustomView's contents are edited, -setValue: doesn't get called when
the preferences are read in off the disk, and even when I manually send
a -setValue: message to try and trigger something the KVO isn't
happening. This same pattern (using -bind:toObject:withKeyPath:options:
and -addObserver:forKeyPath:options:context:) works for other elements
in the preferences UI, but not for this ComplexObject/CustomView pair
with the NSDictionary value.
Is what I am trying to do possible using Cocoa Bindings? And if so,
what do I need to do to get it to work? There are some things in the
docs that I think *might* work, but I am really not sure if I am
barking up the wrong tree... Things like:
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues (in
the NSKeyValueCoding protocol)
- (void)didChangeValueForKey:(NSString *)key (in NSKeyValueObserving
protocol)
- (void)willChangeValueForKey:(NSString *)key (in NSKeyValueObserving
protocol)
There are some threads that may be related, but I can't see the answer
in there...
<
http://www.cocoabuilder.com/archive/message/cocoa/2004/10/3/118770>
<
http://www.cocoabuilder.com/archive/message/cocoa/2004/10/7/119025>
<
http://www.cocoabuilder.com/archive/message/cocoa/2004/10/5/118866>
<
http://www.cocoabuilder.com/archive/message/cocoa/2004/7/29/113114>
<
http://www.cocoabuilder.com/archive/message/2004/10/7/119025>
Any advice much appreciated...
Cheers,
Greg
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden