Key Value Observing?
Key Value Observing?
- Subject: Key Value Observing?
- From: Steve Steinitz <email@hidden>
- Date: Tue, 11 Dec 2007 23:32:25 +1100
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