Generic Instance Method 総称体インスタンスメソッド

registerUndo(withTarget:handler:)

Registers the specified closure to implement a single undo operation that the target receives. 指定されたクロージャを登録して、レシーバを対象とする単一の取り消し操作を実装します。

Declaration 宣言

func registerUndo<TargetType>(withTarget target: TargetType, handler: @escaping (TargetType) -> Void) where TargetType : AnyObject

Parameters パラメータ

target

The target of the undo operation. 取り消し操作の目標。

The undo manager maintains an unowned reference to the target to prevent retain cycles. アンドゥマネージャは、targetに対する非所有参照を保守して、保持循環を防ぎます。

handler

A closure to be executed when an operation is undone. 操作が取り消される時に遂行されるクロージャ。

The closure takes a single argument, the target of the undo operation. ただ1つだけの引数、取り消し操作の目標、をとるクロージャ。

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. registerUndo(withTarget:handler:)を使って、クロージャを取り消し操作として取り消しスタック上に登録してください。登録されたクロージャは、それからundoが呼び出されて取り消し操作が発生する時に遂行されます。目標は参照型である必要があります、そうすることでそれの状態は取り消しまたはやり直しされることがアンドゥマネージャによって可能です。

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. 目標との保持循環を防ぐために、クロージャパラメータ上で演算してください、同じ目標を参照する外部スコープの中の変数上でではなく。例えば、上でのコード出力において、クロージャは$0パラメータ上で演算します、直接にbouquetSelection上でではなく。

See Also 参照

Registering Undo Operations 取り消し操作を登録する

Related Documentation 関連文書