registerUndo(withTarget:handler:)
Registers the specified closure to implement a single undo operation that the target receives.
Declaration
@MainActor @preconcurrency func registerUndo<TargetType>(withTarget target: TargetType, handler: @escaping @MainActor (TargetType) -> Void) where TargetType : AnyObjectParameters
- target:
The target of the undo operation.
The undo manager maintains an unowned reference to the
targetto prevent retain cycles. - handler:
A closure to execute when an operation is undone.
The closure takes a single argument, the target of the undo operation.
Discussion
Use registerUndo(withTarget:handler:) to register a closure as an undo operation on the undo stack. The registered closure is then executed when undo is called and the undo operation occurs. The target needs to be a reference type so that its state can be undone or redone by the undo manager.
The following example demonstrates how you can use this method to register an undo operation that adds an element back into a mutable array.
var manager = UndoManager()
var bouquetSelection: NSMutableArray = ["lilac", "lavender"]
func pull(flower: String) {
bouquetSelection.remove(flower)
manager.registerUndo(withTarget: bouquetSelection) { $0.add(flower) }
}
pull(flower: "lilac")
// bouquetSelection == ["lavender"]
manager.undo()
// bouquetSelection == ["lavender", "lilac"]To avoid retain cycles with the target, operate on the closure parameter rather than on variables in an outer scope that reference the same target. For example, in the code listing above, the closure operates on the $0 parameter rather than directly on bouquetSelection.
See Also
Related Documentation
- Introduction to Undo Architecture