Instance Method インスタンスメソッド

registerUndo(withTarget:selector:object:)

Registers the selector of the specified target to implement a single undo operation that the target receives. 指定目標のセレクタを登録することで、レシーバを目標とするある単一の取り消し操作を実装します。

Declaration 宣言

func registerUndo(withTarget target: Any, 
         selector: Selector, 
           object anObject: Any?)

Parameters パラメータ

target

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

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

aSelector

The selector for the undo operation. 取り消し操作に対するセレクタ。

anObject

The argument sent with the selector. セレクタで送信する引数。

The undo manager maintains a strong reference to anObject. アンドゥマネージャは、anObjectに対する強い参照を保守します。

Discussion 議論

Use registerUndo(withTarget:selector:object:) to register a selector for an undo operation. To register a selector on the undo stack, you also need to make the method available to the Objective-C runtime by applying the @objc attribute to the method. For more on how to create a selector, see Selectors. registerUndo(withTarget:selector:object:)を使って、ある取り消し操作に対するセレクタを登録してください。セレクタを取り消しタスクに登録するには、あなたはまたObjective-Cランタイムに利用可能なメソッドを、@objc属性をそのメソッドに適用することによって作る必要があります。セレクタを作成する方法に関してさらには、セレクタを見てください。

Calling this method also clears the redo stack. このメソッドを呼び出すことはまた、やり直しスタックをすっかりきれいにします。

The following example demonstrates how to register and use a selector on the undo stack by modeling a Garden class with two methods: plant(flower:) and pluck(flower:). The plant(flower:) method removes a flower from the garden while pluck(flower:) adds a flower such that effectively, the two methods are inverse operations of each other. This inverse quality makes it ideal to register plant(flower:) and pluck(flower:) to be each other's undo operation. 以下の例は、セレクタを取り消しスタックに登録して使用する方法を、Gardenクラスを2つのメソッド:plant(flower:)pluck(flower:)でこしらえることで実演します。plant(flower:)メソッドはある花を花壇から取り除きます、一方pluck(flower:)は印象的な花を加えます、2つのメソッドは互いに逆の操作です。この正反対の性質は、plant(flower:)pluck(flower:)を互いのもつ取り消し操作に登録するのに申し分ないものとします。


class Garden {
    typealias FlowerName = String


    var flowers: [FlowerName: Int]
    var manager = UndoManager()


    init(flowers: [FlowerName: Int]) {
        self.flowers = flowers
    }
    @objc func plant(flower: FlowerName) {
        flowers[flower, default: 0] += 1
        manager.registerUndo(withTarget: self, selector: #selector(pluck), object: flower)
    }
    @objc func pluck(flower: FlowerName) {
        flowers[flower, default: 0] -= 1
        manager.registerUndo(withTarget: self, selector: #selector(plant), object: flower)
    }
}
var garden = Garden(flowers: ["orchid" : 2, "lavender": 1, "violet": 2])
garden.pluck(flower: "orchid")
//garden.flowers == ["orchid" : 1, "lavender": 1, "violet": 2]
garden.manager.undo()
//garden.flowers == ["orchid" : 2, "lavender": 1, "violet": 2]

See Also 参照

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

Related Documentation 関連文書