Re: How is a bound NSArrayController so stealthy?
Re: How is a bound NSArrayController so stealthy?
- Subject: Re: How is a bound NSArrayController so stealthy?
- From: Shamyl Zakariya <email@hidden>
- Date: Tue, 5 Jun 2007 17:06:53 -0400
If I'm understanding correctly what your problem is, I encountered
the same thing a couple months back ( in my first "real" use of
bindings ).
The way I worked around it seems hacky, but does work. What I did was
to fake a property on my document ( using a private category ) to
expose setter/getter for the values in my array. The implementations
were empty, btw.
@interface CalcDocument( Private )
//
// The following are three faux properties to track live
// changes to the variables array. I'm not sure if it's a hack, or not.
//
- (void) setVariableNames: (NSArray*) names;
- (NSArray*) variableNames;
- (void) setVariableValues: (NSArray*) values;
- (NSArray*) variableValues;
@end
Then, in my +initialize, I made a dependancy for those fake keys:
+ (void) initialize
{
[self setKeys:
[NSArray arrayWithObjects:
@"variableNames", // faux property
@"variableValues", // faux property
@"variables",
@"expression",
@"currentTrigMode",
@"base",
nil]
triggerChangeNotificationsForDependentKey: @"result"];
}
Finally, in -awakeFromNib, I manually created a binding for those
properties:
//
// Bind variableNames & variableValues to array controller. This might
// be a bit of a hack, but I need to track variable name/value changes
// individually, so to do so I have to bind dummy properties for
these values
// to be able to track changes.
//
[self bind: @"variableNames" toObject: _variablesArrayController
withKeyPath: @"arrangedObjects.name" options: nil];
[self bind: @"variableValues" toObject: _variablesArrayController
withKeyPath: @"arrangedObjects.value" options: nil];
This worked, for me. After doing so, the -result property was
correctly recalculated when the *values* of my variables ( being
managed by an NSArrayController ) were changed.
There may ( and probably is ) a better way to do this, but the above
worked for me!
email@hidden
"obviously, you're not a golfer"
-- the Dude
On Jun 5, 2007, at 2:38 PM, Jerry Krinock wrote:
I have some NSTableColumns bound to an NSArrayController, which is
in turn bound to an NSMutableArray in my model. I'd like a
notification when the user edits an existing item in the table.
In mmalc's ToDos example, after NSLogging all the accessors in
MyDocument.m, I've found that, when the ToDos table is edited, none
of the accessors are invoked. [1] Neither is the array controller
mutating the array returned by (NSArray*)toDos because, after I
modified -(NSArray*)toDos to return an immutable copy, nothing
broke. When I "Save", the toDos returned to
dataRepresentationOfType: and written to the file is complete with
all my edits.
How is NSArrayController able to change the model to which it is
bound so stealthily? I suppose it could be modifying the instance
variable directly a la myDocument->toDos? If so, how can I be
notified? I tried adding self as an observer to key path toDos,
but observeValueForKeyPath:ofObject:change:context: does not get
invoked when I edit an existing table item.
Jerry Krinock
(The reason I want this notification is because I'm having trouble
getting an array to update the model in a "dotted" key path such as
someObject.myDocument.toDos, and while searching for a workaround
on that I discovered this issue. I'm sure it would help immensely
if I understood how this worked.)
[1] None of the following ever gets invoked:
- (void)setToDos:(NSMutableArray *)aToDos
- (unsigned int)countOfToDos
- (id)objectInToDosAtIndex:(unsigned int)index
- (void)removeObjectFromToDosAtIndex:(unsigned int)index
- (void)replaceObjectInToDosAtIndex:(unsigned int)index withObject:
(id)anObject
This one gets invoked when adding a new item, but not when editing
an existing item:
- (void)insertObject:(id)anObject inToDosAtIndex:(unsigned int)index
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden