Class

UndoManager

A general-purpose recorder of operations that enables undo and redo.

Overview

You register an undo operation by calling one of the methods described in Registering Undo Operations. You specify the name of the object that’s changing (or the owner of that object) and provide a closure, method, or invocation to revert its state.

After you register an undo operation, you can call undo() on the undo manager to revert to the state of the last undo operation. When undoing an action, UndoManager saves the operations you reverted to so that you can call redo() automatically.

UndoManager is typically used by apps with UI interactions. For example, UIKit implements undo and redo in its text view object, making it easy for you to undo and redo actions in objects along the responder chain. UndoManager also serves as a general-purpose state manager, which can be used to undo and redo man kinds of actions. For example, an interactive command-line utility could use this class to undo the last command run, or a networking library could undo a request by sending another request that invalidates the previous one.

Topics

Registering Undo Operations

func registerUndo<TargetType>(withTarget: TargetType, handler: (TargetType) -> Void)

Records a single undo operation for a given target so that when an undo is performed, it executes the specified closure.

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’s sent a specified selector with a given object as the sole argument.

func prepare(withInvocationTarget: Any)

Prepares the undo manager for invocation-based undo with the given target as the subject of the next undo operation.

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

let NSUndoCloseGroupingRunLoopOrdering: Int

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

let NSUndoManagerGroupIsDiscardableKey: String

The key has a corresponding value of true, wrapped as a Boolean NSNumber object, if the undo group as a whole is discardable.

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