Class

Undo​Manager

NSUndo​Manager 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 NSUndo​Manager saves the operations reverted so that you can redo the undos.

NSUndo​Manager 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 register​Undo(with​Target:​ 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(with​Invocation​Target:​ 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 can​Undo:​ Bool

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

var can​Redo:​ 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 undo​Nested​Group().

func undo​Nested​Group()

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 levels​Of​Undo:​ Int

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

Creating Undo Groups

func begin​Undo​Grouping()

Marks the beginning of an undo group.

func end​Undo​Grouping()

Marks the end of an undo group.

var groups​By​Event:​ Bool

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

var grouping​Level:​ 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 disable​Undo​Registration()

Disables the recording of undo operations, whether by register​Undo(with​Target:​selector:​object:​) or by invocation-based undo.

func enable​Undo​Registration()

Enables the recording of undo operations.

var is​Undo​Registration​Enabled:​ Bool

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

Checking Whether Undo or Redo Is Being Performed

var is​Undoing:​ Bool

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

var is​Redoing:​ Bool

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

Clearing Undo Operations

func remove​All​Actions()

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

func remove​All​Actions(with​Target:​ 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 undo​Action​Name:​ String

The name identifying the undo action.

var redo​Action​Name:​ String

The name identifying the redo action.

func set​Action​Name(String)

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

Getting and Localizing the Menu Item Title

var undo​Menu​Item​Title:​ String

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

var redo​Menu​Item​Title:​ String

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

func undo​Menu​Title(for​Undo​Action​Name:​ String)

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

func redo​Menu​Title(for​Undo​Action​Name:​ String)

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

Working with Run Loops

var run​Loop​Modes:​ [Run​Loop​Mode]

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

Discardable Undo and Redo Actions

func set​Action​Is​Discardable(Bool)

Sets whether the next undo or redo action is discardable.

var undo​Action​Is​Discardable:​ Bool

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

var redo​Action​Is​Discardable:​ Bool

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

Constants

NSUndo​Close​Grouping​Run​Loop​Ordering

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

Notifications

static let NSUndo​Manager​Checkpoint:​ NSNotification.Name

Posted whenever an NSUndo​Manager object opens or closes an undo group (except when it opens a top-level group) and when checking the redo stack in can​Redo.

static let NSUndo​Manager​Did​Open​Undo​Group:​ NSNotification.Name

Posted whenever an NSUndo​Manager object opens an undo group, which occurs in the implementation of the begin​Undo​Grouping() method.

static let NSUndo​Manager​Did​Redo​Change:​ NSNotification.Name

Posted just after an NSUndo​Manager object performs a redo operation (redo()).

static let NSUndo​Manager​Did​Undo​Change:​ NSNotification.Name

Posted just after an NSUndo​Manager object performs an undo operation.

static let NSUndo​Manager​Will​Close​Undo​Group:​ NSNotification.Name

Posted before an NSUndo​Manager object closes an undo group, which occurs in the implementation of the end​Undo​Grouping() method.

static let NSUndo​Manager​Did​Close​Undo​Group:​ NSNotification.Name

Posted after an NSUndo​Manager object closes an undo group, which occurs in the implementation of the end​Undo​Grouping() method.

static let NSUndo​Manager​Will​Redo​Change:​ NSNotification.Name

Posted just before an NSUndo​Manager object performs a redo operation (redo()).

static let NSUndo​Manager​Will​Undo​Change:​ NSNotification.Name

Posted just before an NSUndo​Manager object performs an undo operation.

Relationships

Inherits From