I. Savant wrote:
>> Check out this page:
>>
>> http://homepage.mac.com/mmalc/CocoaExamples/controllers.html
>>
> 
>   I should also mention that you should approach these examples like this:
> 
> 1 - Read the Cocoa Bindings documentation (including all the Key Value
> Coding / Key Value Observing related documentation).
> 2 - Examine the examples given above.
> 3 - Go back over the documentation!!! Seeing a working example makes a
> second perusal of the docs much more fruitful.

Thanks a lot for your remarks, I really appreciate it. My basic
implementation already works!

I have built a master-detail window, using an NSTable view for label
and amount. My PieChartView is observing the array like this:

> - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
>                       change:(NSDictionary *)change context:(void *)context
> {
>     NSLog(@"observeValueForKeyPath:%@ ofObject:%p %@", keyPath, object, 
> change);
> 
>     int i;
>     NSArray *changedObjects;
>     int changeType = [[change valueForKey:NSKeyValueChangeKindKey] intValue];
> 
>     switch (changeType) {
>       case NSKeyValueChangeInsertion:
>           changedObjects = [change valueForKey:NSKeyValueChangeNewKey];
>           for (i = 0; i < [changedObjects count]; i++) {
>               [self observeObject:[changedObjects objectAtIndex:i] 
> withKeyPath:@"label"];
>               [self observeObject:[changedObjects objectAtIndex:i] 
> withKeyPath:@"amount"];
>           }
>           break;

This is an excerpt of the code executed whenever a new row is added to
the NSTableView. I observe the new NSDictionary provided by
NSArrayController. Deleting multiple rows also works.

I'd like to show you one ugly detail. I put all observed objects,
including their keyPath in a local NSDictionary, because I have to
remove the observers on deletion of a table row. The 'observeObject'
looks like that:

> - (void)observeObject:(id)object withKeyPath:(NSString *)keyPath {
>     NSLog(@"start observing %p with keypath %@", object, keyPath);
>     
>     [object addObserver:self forKeyPath:keyPath
>               
> options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld 
> context:nil];    
> 
>     [observers setValue:object forKeyPath:[NSString 
> stringWithFormat:@"%p.%@", object, keyPath]];
> }

In my opinion, this is as ugly as it can possibly get.

I'm using the address of the observed object and append the keypath to
create a pseudo key for each observed instance.

I don't like that at all, but how would I be able to remove all
observers when I have to clean them up later?

Is there a better approach instead of observing every property of all
array entries?

Patrick

_______________________________________________

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 [EMAIL PROTECTED]

Reply via email to