On 14 Mar 2012, at 20:12, Seth Willits wrote:

> 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.
> 


By "strong reference", I assume you mean I should add an ivar of type 
NSUndoManager to my model object to hold a reference to the document's 
undoManager, right? If so, I think I understand what you are saying. I will 
give this a try. Thanks!

But could you also please explain a little more clearly what you mean with 
"weak reference" to the document?

-- 
Luc Van Bogaert




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to