Re: Strategy for acting on changes in child objects?
Re: Strategy for acting on changes in child objects?
- Subject: Re: Strategy for acting on changes in child objects?
- From: Graham Cox <email@hidden>
- Date: Thu, 7 May 2009 15:27:29 +1000
On 07/05/2009, at 3:18 PM, Seth Willits wrote:
This happens often enough that I need a real plan. I typically have
parent objects with child objects that have attributes bound to
different things in a GUI or possibly even unbound and simply
modified by something else. The parent object needs to perform some
type of action whenever one of the (possibly) many attributes of the
child object changes, such as update a file containing the data the
child represents.
I'm looking for a strategy to handle watching those changes. So far
I've used a few odd solutions which generally stem from overriding
setValue:forKey: (or each accessor explicitly depending on the
situation) in the child. I've used that to trigger an single key
(isModified) which the parent watches, post a notification, call a
delegate method, and probably some others. None of which I like.
Suggestions?
KVO?
It is designed for exactly this, in general (the fact that it is also
useful to link models to views is an added bonus).
The parent gets to decide which attributes it's interested in, and
registers itself as a KVO observer for those attributes. Then it does
whatever it needs when the KVO observation method of the parent is
called. The parent stops observing when a child is removed, or when it
is deallocated.
This works well for things like centralising Undo for example, so it's
not just a GUI technique. Hillegasse covers this in his book.
--Graham
_______________________________________________
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