Contents

UndoManager

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

Declaration

@MainActor class UndoManager

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 revert to so that you can call redo() automatically.

Typically, apps with UI interactions work with UndoManager. 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 you can use to undo and redo many kinds of actions. For example, an interactive command-line utility can use this class to undo the last command run, or a networking library can undo a request by sending another request that invalidates the previous one.

Topics

Registering undo operations

Checking undo ability

Performing undo and redo

Managing undo and redo stack depth

Creating undo groups

Enabling and disabling undo

Checking whether undo or redo is in process

Clearing undo operations

Managing the action name

Getting and localizing the menu item title

Working with user info

Working with run loops

Using discardable undo and redo actions

Working with notifications

Working with notification messages