forwarding KVO from NSDocument to model
forwarding KVO from NSDocument to model
- Subject: forwarding KVO from NSDocument to model
- From: Cameron Hayne <email@hidden>
- Date: Wed, 11 May 2005 04:36:56 -0400
In the Currency Converter example (bindings version at http://
developer.apple.com/documentation/Cocoa/Conceptual/
CurrencyConverterBindings/index.html), if I add a timer that
increases the exchangeRate periodically, I do see the changes
reflected automatically in the view.
But I wanted to change this example so that the model object (that
holds the values of exchangeRate etc) is not in the NIB file but
rather is created programmatically by the NSDocument.
I added the following methods to my NSDocument sub-class to forward
the KVC requests to the model.
- (id)valueForUndefinedKey:(NSString *)key
{
NSLog(@"MyDocument.valueForUndefinedKey %@", key);
return [model valueForKey: key];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
NSLog(@"MyDocument.setValue: %@ forUndefinedKey %@", value, key);
[model setValue: value forKey: key];
}
I removed the "Converter" (model object) from the NIB and connected
the NSObjectController's "content" outlet to the File's Owner.
This works fine as far as changes made on the view being forwarded to
the model.
But the changes to the model made via the timer are not reflected on
the view.
It is obvious why - it is because it is the NSDocument that is being
observed, not the model.
However, it is far from obvious how I should go about setting up the
"forwarding" of KVO so that the model changes will get observed. I
don't want to have to add an observer for each and every model
attribute - but I don't see a nice way to handle it otherwise.
I guess I must be missing something in the KVO protocol.
Can anyone point me to an example that does something like this? I've
looked at mmalc's examples but there doesn't seem to be anything like
this.
--
Cameron Hayne
email@hidden
_______________________________________________
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