Class

UndoManager

NSUndoManager is a general-purpose recorder of operations for undo and redo.

Overview

You register an undo operation by specifying the object that’s changing (or the owner of that object), along with a method to invoke to revert its state, and the arguments for that method. When performing undo an NSUndoManager saves the operations reverted so that you can redo the undos.

NSUndoManager is implemented as a class of the Foundation framework because executables other than applications might want to revert changes to their states. For example, you might have an interactive command-line tool with undo and redo commands, or there could be distributed object implementations that can revert operations “over the wire.” However, users typically see undo and redo as application features. UIKit implements undo and redo in its text view object and makes it easy to implement it in objects along the responder chain (see UIResponder).

Symbols

Registering Undo Operations

func registerUndo(withTarget: Any, selector: Selector, object: Any?)

Records a single undo operation for a given target, so that when an undo is performed it is sent a specified selector with a given object as the sole argument.

func prepare(withInvocationTarget: Any)

Prepares the receiver for invocation-based undo with the given target as the subject of the next undo operation and returns self.

Checking Undo Ability

var canUndo: Bool

A Boolean value that indicates whether the receiver has any actions to undo.

var canRedo: Bool

A Boolean value that indicates whether the receiver has any actions to redo.

Performing Undo and Redo

func undo()

Closes the top-level undo group if necessary and invokes undoNestedGroup().

func undoNestedGroup()

Performs the undo operations in the last undo group (whether top-level or nested), recording the operations on the redo stack as a single group.

func redo()

Performs the operations in the last group on the redo stack, if there are any, recording them on the undo stack as a single group.

Limiting the Undo Stack

var levelsOfUndo: Int

The maximum number of top-level undo groups the receiver holds.

Creating Undo Groups

func beginUndoGrouping()

Marks the beginning of an undo group.

func endUndoGrouping()

Marks the end of an undo group.

var groupsByEvent: Bool

A Boolean value that indicates whether the receiver automatically creates undo groups around each pass of the run loop.

var groupingLevel: Int

The number of nested undo groups (or redo groups, if Redo was invoked last) in the current event loop.

Enabling and Disabling Undo

func disableUndoRegistration()

Disables the recording of undo operations, whether by registerUndo(withTarget:selector:object:) or by invocation-based undo.

func enableUndoRegistration()

Enables the recording of undo operations.

var isUndoRegistrationEnabled: Bool

A Boolean value that indicates whether the recording of undo operations is enabled.

Checking Whether Undo or Redo Is Being Performed

var isUndoing: Bool

Returns a Boolean value that indicates whether the receiver is in the process of performing its undo() or undoNestedGroup() method.

var isRedoing: Bool

Returns a Boolean value that indicates whether the receiver is in the process of performing its redo() method.

Clearing Undo Operations

func removeAllActions()

Clears the undo and redo stacks and re-enables the receiver.

func removeAllActions(withTarget: Any)

Clears the undo and redo stacks of all operations involving the specified target as the recipient of the undo message.

Managing the Action Name

var undoActionName: String

The name identifying the undo action.

var redoActionName: String

The name identifying the redo action.

func setActionName(String)

Sets the name of the action associated with the Undo or Redo command.

Getting and Localizing the Menu Item Title

var undoMenuItemTitle: String

The complete title of the Undo menu command, for example, “Undo Paste.”

var redoMenuItemTitle: String

The complete title of the Redo menu command, for example, “Redo Paste.”

func undoMenuTitle(forUndoActionName: String)

Returns the complete, localized title of the Undo menu command for the action identified by the given name.

func redoMenuTitle(forUndoActionName: String)

Returns the complete, localized title of the Redo menu command for the action identified by the given name.

Working with Run Loops

var runLoopModes: [RunLoopMode]

The modes governing the types of input handled during a cycle of the run loop.

Discardable Undo and Redo Actions

func setActionIsDiscardable(Bool)

Sets whether the next undo or redo action is discardable.

var undoActionIsDiscardable: Bool

Boolean value that indicates whether the next undo action is discardable.

var redoActionIsDiscardable: Bool

Boolean value that indicates whether the next redo action is discardable.

Constants

NSUndoCloseGroupingRunLoopOrdering

NSUndoManager provides this constant as a convenience; you can use it to compare to values returned by some NSUndoManager methods.

Notifications

static let NSUndoManagerCheckpoint: NSNotification.Name

Posted whenever an NSUndoManager object opens or closes an undo group (except when it opens a top-level group) and when checking the redo stack in canRedo.

static let NSUndoManagerDidOpenUndoGroup: NSNotification.Name

Posted whenever an NSUndoManager object opens an undo group, which occurs in the implementation of the beginUndoGrouping() method.

static let NSUndoManagerDidRedoChange: NSNotification.Name

Posted just after an NSUndoManager object performs a redo operation (redo()).

static let NSUndoManagerDidUndoChange: NSNotification.Name

Posted just after an NSUndoManager object performs an undo operation.

static let NSUndoManagerWillCloseUndoGroup: NSNotification.Name

Posted before an NSUndoManager object closes an undo group, which occurs in the implementation of the endUndoGrouping() method.

static let NSUndoManagerDidCloseUndoGroup: NSNotification.Name

Posted after an NSUndoManager object closes an undo group, which occurs in the implementation of the endUndoGrouping() method.

static let NSUndoManagerWillRedoChange: NSNotification.Name

Posted just before an NSUndoManager object performs a redo operation (redo()).

static let NSUndoManagerWillUndoChange: NSNotification.Name

Posted just before an NSUndoManager object performs an undo operation.

Relationships

Inherits From

Conforms To