The Application Kit supplements the behavior of NSUndoManager
in several ways:
It offers default undo and redo behavior in text.
It includes APIs for managing the action names that appear with “Undo” and “Redo” in an application’s menu.
It establishes a framework for the distribution and selection of undo managers in an application
As application can have one or more undo clients—objects that register and perform undo operations in their local contexts. Each of these objects has its own NSUndoManager
object and the associated undo and redo stacks. One example of this scenario involves custom views, each a client of an undo manager. For example, you could have a window with two custom views; each view can display text in changeable attributes (such as font, color, and size) and users can undo (or redo) each change to any attribute in either of the views. NSResponder
and NSWindow
define methods to help you control the context of undo operations within the view hierarchy.
NSResponder
declares the undoManager
method for most objects that inherit from it (namely, windows and views). When the first responder of an application receives an undo
or redo
message, NSResponder
goes up the responder chain looking for a next responder that returns an NSUndoManager
object from undoManager
. Any returned undo manager is used for the undo or redo operation.
If the undoManager
message wends its way up the responder chain to the window, the NSWindow
object queries its delegates with windowWillReturnUndoManager:
to see if the delegate has an undo manager. If the delegate does not implement this method, the window creates an NSUndoManager
object for the window and all its views.
Document-based applications often make their NSDocument
objects the delegates of their windows and have them respond to the windowWillReturnUndoManager:
message by returning the undo manager used for the document. These applications can also make each NSWindowController
object the delegate of its window—the window controller implements windowWillReturnUndoManager:
to get the undo manager from its document and return it:
return [[self document] undoManager]; |
Instances of NSTextView
provide undo and redo behavior. This is an optional feature, and you must make sure that when you create the text view either you select the appropriate check box in Interface Builder, or send it setAllowsUndo:
with an argument of YES
. If you want a text view to use its own undo manager (and not the window’s), you provide a delegate for the text view; the delegate can then return an instance of NSUndoManager
from the undoManagerForTextView:
delegate method.
The default undo and redo behavior applies to text fields and text in cells as long as the field or cell is the first responder (that is, the focus of keyboard actions). Once the insertion point leaves the field or cell, prior operations cannot be undone.
Last updated: 2009-07-27