3

I have questions about the NSUndoManager methods

  • registerUndoWithTarget:selector:object:

  • prepareWithInvocationTarget:

Usually in examples, the "target" is a controller object that manages all the model objects (usually in an array).

But can the target be an individual model object - the model object to be changed by the undo/redo operation?


The guide says about registerUndoWithTarget:selector:object::

The target object may not be the actual object whose state is changing; instead, it may be the client object, a document or container that holds many undoable objects.

Why this restriction? It is not mentioned in the documentation for the method itself. Also, no such restriction is mentioned for the prepareWithInvocationTarget: method.

Chris Devereux
  • 5,453
  • 1
  • 26
  • 32
dan-3
  • 73
  • 3
  • 1
    If you delete an object and used that as a target - how could the undo work if the target is now gone? You'll need some other instance orchestrating the operation **for** that object that's actually changed – cacau Feb 27 '14 at 10:46

1 Answers1

1

The docs you quote are a bit ambiguous. I would read this:

The target object may not be the actual object whose state is changing; instead, it may be the client object, a document or container that holds many undoable objects.

as this:

The target object might not be the actual object whose state is changing; instead, it may be the client object, a document or container that holds many undoable objects

However, it's usually best not to use a model as the target. The target is not retained, so it could be deallocated while still referenced on the undo stack. Targeting a controller that owns the undo manager is a good idea.

Chris Devereux
  • 5,453
  • 1
  • 26
  • 32