Re: binding 101: controller does not notify?
Re: binding 101: controller does not notify?
- Subject: Re: binding 101: controller does not notify?
- From: Ondra Cada <email@hidden>
- Date: Tue, 14 Mar 2006 20:57:01 +0100
Andre,
On 13.3.2006, at 22:48, email@hidden wrote:
sorry if this is indeed trivial, but somehow I cannot find an
answer...
I've got an NSArrayController subclass
Terribly sorry, I have mis-typed here: the class in question is an
NSTreeController subclass, not an NSArrayController one. Perhaps,
given the well-known bugginess of the NSTreeController, that might be
the culprit?
which happens to have a few derived keys. I would bet that if
programmed this way, it should work:
@interface MyController:NSArrayController .... @end
@implementation MyController
-(NSString*)displayNameOfCurrent {
return [NSString stringWithFormat:@"%d",[self
selectedObjects]]; // just testing, the real case more complex
}
+(void)initialize {
[self setKeys:[NSArray arrayWithObject:@"selectedObjects"]
triggerChangeNotificationsForDependentKey:@"displayNameOfCurrent"];
}
@end
Alas it does not--the KVO notification for displayNameOfCurrent is
not sent if selection changes.
What obvious thing am I overlooking?!?
Perhaps Implement :
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
if ( [key isEqualToString:@"selectedObjects"] )
return YES;
return [super automaticallyNotifiesObserversForKey:key];
}
Thanks, but I fear not: unless I am much mistaken, automatic
notifying has completely nothing to do with the (in my case
inexplicably nonfunctional) triggerChangeNotificationsForDependentKey.
Automatic notifying just means that some isa-swizzle trick catches
magically your setFoo: whenever sent, and automatically calls
willChangeValueForKey:@"foo" before calling it, and
didChangeValueForKey:@"foo" after calling it. That's all.
On the other hand, setKeys:"@foo"
triggerChangeNotificationsForDependentKey:@"bar" says that whenever a
notification for "foo" is sent (regardless whether caused by
automatic or by explicit didChange... call), another notification for
"bar" should be sent, too.
Therefore, given the selectedObjects notification is sent all right
(which it is), there is no point in setting automatic notification
for it.
I believe that NSArrayController does not automatically notify for
some keys by default, so you have to set this up manually. (I know
this is true for arrangedObjects)
I would suppose controllers would automatically notify for nearly
nothing, since they are (presumed to be :)) optimised for maximal
efficiency, and manual notifications tend to be more efficient than
automatic ones. Anyway, it seems irrelevant to the problem.
Good Luck
Thanks... well, so far, the only solution I have any luck with is the
aforementioned work-around :(
Note: at the moment I am using the following work-around which
runs well, but is pretty ugly...
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)
object change:(NSDictionary*)change context:(void*)context {
if ([keyPath isEqualToString:@"selectedObjects"]) {
[self willChangeValueForKey:@"displayNameOfCurrent"];
[self didChangeValueForKey:@"displayNameOfCurrent"];
}
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
-(NSString*)displayNameOfCurrent {
static first=YES;
if (first) {
[self addObserver:self forKeyPath:@"selectedObjects"
options:0 context:NULL];
first=NO;
}
...
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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