RE: Key Value Observing?
RE: Key Value Observing?
- Subject: RE: Key Value Observing?
- From: Todd Blanchard <email@hidden>
- Date: Tue, 11 Dec 2007 16:37:30 -0800
You might look at this: http://objectiveclips.com
There is a sample that does just this. Disclaimer, I wrote it.
-Todd Blanchard
On Dec 11, 2007, at 4:32 AM, Steve Steinitz wrote:
Hello,
My Core Data (not document-based) Point-of-Sale App has Sales which
have
LineItems which have price, quantity and a calculated total. The
LineItems are bound to an NSArrayController which is displayed in an
NSTableView. Sale calculates a total of its LineItems totals. The
Sales
total is displayed in an NSTextView on the Sale window.
Currently, when a user changes a LineItem's price or quantity the
displayed Sales total is not automatically updated. However, when the
user changes the price or quantity and then clicks on another LineItem
in the LineItem NSTableView, the Sales total *is* updated.
After trying and failing to make the update automatic I added a
'Recalculate' button to my Sale Window which calls arrangeObjects
on the LineItem array controller. Not nice.
What is the best way of making the Sales Total update automatically
whenever the LineItem salePrice or quantity change?
Having studied Apple's KVO docs, Googled KVO and examined mmalc's
instructive GraphicBindings application, I still can't grasp how to
automatically update the Sale total when a LineItem attribute changes.
For example, I've added the following to LineItem:
+ (void)
initialize
{
[self setKeys: [NSArray arrayWithObjects: @"quantity",
@"salePrice",
nil]
triggerChangeNotificationsForDependentKey: @"total"];
}
but couldn't figure out a way to add something similar to Sale, like:
+ (void)
initialize
{
[self setKeys: [NSArray arrayWithObjects: @"lineItems",
nil]
triggerChangeNotificationsForDependentKey: @"total"]; // sale
total
}
which might not even be valid and even if it were, it doesn't look
deeper into the lineitem attributes: quantity and salePrice.
Of course, my application is actually more complicated than I've
indicated and although the default KVO works amazingly well, I have
similar update issues elsewhere.
Looking at mmalc's GraphicBindings app, I get a sense that I could
solve
the problem if I were willing to subclass NSArrayController for each
entity that has to-many relationships. But, I already have a dozen
NSArrayController subclasses (to provide custom filtering).
Subclassing
them further would seem excessive.
I'm also hesitant to subclass NSTextView for this purpose -- it
doesn't
seem that it should be necessary.
I've found posts similar to this one in the archives of this list and
others but have not found answers. Any guidance would be appreciated.
Cheers,
Steve
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden