0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UndoManagerで簡単に元に戻す メモ

Posted at

UndoManagerとは

UndoManagerとはUndo機能を実装するためのクラス

第一引数でUndoの対象となるオブジェクトを設定し、第二引数でUndoをした際の挙動をクロージャー内で実行できる。

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

実際のコード

func register() {
   
    // Undo操作をhandlerに登録
    undoManager.registerUndo(withTarget: 対象のオブジェクト) {
        // クロージャの第一引数 $0 は、Targetで指定したオブジェクトが入ってくる。


    }
}

実際にUndoする。先ほどクロージャー内で設定したundo処理を行う。

undoManager.undo()

その他
registerUndoの引数には循環参照を防ぐために参照型しかいれてはいけない。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?