Matthieu Riegler
Matthieu Riegler

Reputation: 55152

KVO on adding/removing objects through NSArrayController

How can I get a notification when an object has been added/removed through an NSArrayController ?

I tried something like

    [core addObserver:self forKeyPath:@"arrangedObjects" options:0 context:nil];

I do get a notification of something has been added or remove but I don't know which object and I don't know what has been done (removing or adding).

The change dictionary of observeValueForKeyPath:ofObject:change:context: doesn't return any useful information.

Upvotes: 3

Views: 298

Answers (1)

Oath Comrade
Oath Comrade

Reputation: 293

I would bind a NSMutableArray to the NSArrayController's contentArray and observe that.

@property (retain) NSMutableArray *array;

[self addObserver:self forKeyPath:@"array" options:NSKeyValueObservingOptionNew context:NULL];

To get the changed index, you can use

[(NSIndexSet *)[[change allValues] lastObject] lastIndex];

Upvotes: 1

Related Questions