Re: Undomanager for custom object?
Re: Undomanager for custom object?
- Subject: Re: Undomanager for custom object?
- From: Seth Willits <email@hidden>
- Date: Wed, 14 Mar 2012 12:12:35 -0700
On Mar 14, 2012, at 3:26 AM, Luc Van Bogaert wrote:
> I would like to implement undo/redo for a model object which in turn is also a ivar backed property of a document object. I'm already using the document's undo manager to undo/redo setting the model object property. Once the property is set, the application has features to change properties of the model object. I'm not sure how to implement undo/redo for the changes of the model object's properties and ivars.
My bad. I completely misinterpreted your original post.
You can use the document's undo manager to hold the changes to your object. In your model object, you'll need a either a weak reference to the document, or a strong reference to an undo manager. Let's assume the latter. When you set the custom object property on the document, set that object's undo manager to that of the document. (Unset it off of the custom object you're replacing.) Inside your custom object's setter methods, you'd handle the undo like normal:
- (void)setFoo:(int)foo
{
[[[self undoManager] prepareInvocationWithTarget:self] setFoo:_foo];
_foo = foo;
}
This way both changes to the document and to the custom object are in the same undo manager which is attached to the document's window.
Side notes: If you're wondering about having the undo registration in the model's setter itself, by doing so you never need a third object to properly change the value AND register the undo. To me it felt a little weird to have a model have an undo manager since conceptually it seemed to me that should be handled at a controller level, but it really does simply the coding, and after a whole lot of searching the archives and reading apple documentation it seems to be the preferred solution in many cases. It doesn't always have to be that way.
I hope that helps,
--
Seth Willits
_______________________________________________
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