Re: Modifying the array of an NSArrayController
Re: Modifying the array of an NSArrayController
- Subject: Re: Modifying the array of an NSArrayController
- From: Spook <email@hidden>
- Date: Thu, 6 Nov 2003 19:14:15 -0600
Thanks Rene'!!!!
Considering the Documentation Layer is under the NDA until 10.4, it's
nice to have someone ferret this information out for outer developer
party.
:-)
Hello Reni,
actually i have only access to the header files so i cant validate my
next statements.
Please try and let me know what happend.
if your document has a toManyProperty called "foos" you will have the
following methods:
-(NSMutableArray *)foos;
-(void)setFoos:(NSMutableArray *)newFoos;
You bind "contentArray" from the NSArrayController to "foos". The
controller is know watching for
changes of that property. But if you add objects to the foos array the
property is not changing !!!!!
It's always the same array. If you don't call setFoos: on your
document no notification will be posted.
You have to be KeyValueCoding and KeyValueObserving compliant in your
code.
Some magic glue code has to post the necessary notifications !!!!
See NSKeyValueCoding.h and NSKeyValueObserving.h !!!
(1)So try this:
NSMutableArray *a = [self foos];
[a addObject:@"Test"];
[self setFoos:[a mutableCopy]];
(2)or this :
NSMutableArray *a = [self mutableArrayValueForKey:@"foos"];
[a addObject:@"Test"];
(3)or this :
[self willChangeValueForKey:@"foos"];
[[self foos] addObject:@"Test"];
[self didChangeValueForKey:@"foos"];
(4)or this :
Implement the following two methods on your document:
- (void)insertObject:(id)anObject inFoosAtIndex:(unsigned)index {
[[self foos] insertObject:anObject atIndex:index];
}
- (void)removeObjectFromFoosAtIndex:(unsigned)index {
[[self foos] removeObjectAtIndex:index];
}
and try :
[self insertObject:@"Test" inFoosAtIndex:[[self foos] count]];
Please let me know what happend. I think example number 2 is the way
the NSArrayController is getting
access to your property. Example 2 is using example number 4 if
available.
That's what i can read from the headers.
Regards,
Henrik
P.S.: Scott , you can't observe the file's owner or an array. you can
only observe properties. You can't observe
an array by it self, but you can observe a property which returns an
array. Only to get things right :-)
You always need a key/keyPath to observe something.
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?
On Nov 6, 2003, at 2:36 PM, Reni Puls wrote:
Hello Henrik,
thanks for your quick reply. I think you pointed me in the right
direction, but I still didn't have any success.
NSControllers only register for changenotifications if you BIND to
the value you want the controller to observe.
Like the NSControll subclasses (NSTextField,NSButton,...) which you
would bind to somthing inside a NSController you have
to bind the "contentArray" binding of the NSArrayController;
If your NSArray is a property of your FilesOwner you can establish
this binding with IB.
Access to this property must be key value coding-compliant in your
App.
Here is what I have now:
The "contentArray" key of NSArrayController is bound to the array
through the File's Owner, which is my NSDocument subclass. But
again, changes to the array are not reflected in the view until I
use [arrayController rearrangeObjects]. If I do call this method,
the items appear in the table view. So the binding must
work--correct?
Just for fun, I tried to register as an observer for the array
myself. But what would be the correct key path to observe? I tried
something like:
[array addObserver:self forKeyPath:@"count"
options:NSKeyValueObservingOptionNew context:NULL];
But this will raise an exception:
<NSInvalidArgumentException> [<NSCFArray 0x3009a0>
addObserver:forKeyPath:options:context:] is not supported. Key
path: count
I get the feeling that what I am trying to is not really supported.
Are there any example applications which directly manipulate the
array of an NSArrayController? Or could it be possible that the
designers never intended it to work like that?
Kind regards,
Reni 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.
_______________________________________________
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.
_______________________________________________
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.