Re: Modifying the array of an NSArrayController
Re: Modifying the array of an NSArrayController
- Subject: Re: Modifying the array of an NSArrayController
- From: René Puls <email@hidden>
- Date: Thu, 6 Nov 2003 23:27:24 +0100
Am 06.11.2003 um 21:58 schrieb Scott Anguish:
If you observe the File's Owner (MyDocument in your case?) then you
can register for the key that contains the array.
Note that observing an array doesn't notify you of changes to
attributes within objects in the array, only changes to the array.
Make sense?
Theoretically yes, but it still didn't solve my problem. :-)
I used this to observe changes to my own array:
[self addObserver:self
forKeyPath:@"array"
options:NSKeyValueObservingOptionNew
context:NULL];
But when I added anything to this array, the
observeValueForKeyPath:ofObject:change:context: method did not get
called.
To make sure nothing else was causing the problem, I just created a
new, minimal project from scratch, a document-based Cocoa application.
The MyDocument class looks like this:
@interface MyDocument : NSDocument
{
IBOutlet NSArrayController *arrayController;
NSMutableArray *array;
}
- (IBAction)addItem:(id)sender;
- (IBAction)refresh:(id)sender;
@end
In [MyDocument init] I create the array:
array = [[NSMutableArray alloc] init];
In [MyDocument addItem:] I add a new item to the Array:
[array addObject:[NSDictionary dictionaryWithObject:@"New Item"
forKey:@"string"]];
And in [MyDocument refresh:] I use:
[arrayController rearrangeObjects];
MyDocument.nib contains an NSArrayController and a single window with
one NSTableView. I have set the object class name of the array
controller to NSDictionary and bound "contentArray" to "File's Owner
(MyDocument)", Model Key Path "array". The arrayController outlet of
File's Owner (and thus MyDocument) is connected to the
NSArrayController.
The NSTableView has only one column, identifier "string", "value" bound
to the ArrayController, controller key "arrangedObjects", model key
path "string".
The window also contains two buttons which are connected to the
addItem: and refresh: actions in MyDocument, respectively.
I hope I didn't forget anything. Now I compile and run the application,
then click on the addObject: button multiple times. The table view is
not updated. I click on the refresh: button, and suddenly all items
appear.
A .zip file of the whole project can be downloaded here:
http://home.kcore.de/~kianga/public/ArrayControllerDemo.zip (23 KB)
If anyone can point out the error, I'll be very very happy. :-)
Kind regards,
Rene Puls
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.