[cocoa][swift]NSUndoManager
NSUndoManagerの利用は、Swiftで楽になったと思うが、その仕組みが見えにくくなったと思うので、Objective-Cの場合から説明する。
CocoaのUndoとRedoは、NSInvocationというクラスでNSObjectの子クラスとメソッドを保持し、それをNSUndoManager内のスタックで管理することで実現している。
なんらかの操作を行うと、Undoに必要なNSInvocationのインスタンスがUndoスタックに積まれていく。
ユーザがUndoを行うと、Redoに必要なNSInvocationのインスタンスがRedoスタックに積まれていく。
Objective-Cのコードで、以下のようになる。
- (void)makeItHotter
{
temperature = temperature + 10;
[[undoManager prepareWithInvocationTarget:self] makeItColder];
}
- (void)makeItColder
{
temperature = temperature - 10;
[[undoManager prepareWithInvocationTarget:self] makeItHotter];
}
makeItHotterで温度を10度上げて、makeItColderをNSUndoManagerに積む。
makeItColderでは、温度を10度下げて、makeItHotterをNSUndoManagerに積む。
これをSwiftを書く場合、積むメソッドのselectorを用意するのが面倒になるのだが、selectorを必要としないメソッドが用意されていた。以下のようになる。
func makeItHotter() {
var temperature = self.textField.intValue
temperature = temperature + 10
self.undoManager?.registerUndo(withTarget: self, handler: {
vc in
vc.makeItColder()
})
self.textField.intValue = temperature
}
func makeItColder() {
var temperature = self.textField.intValue
temperature = temperature - 10
self.undoManager?.registerUndo(withTarget: self, handler: {
vc in
vc.makeItHotter()
})
self.textField.intValue = temperature
}
ソースコード
GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/mac/Temperature - GitHub
【関連情報】
Cocoa Programming for Mac OS X
Cocoa.swift
Cocoa勉強会 関東
Cocoa練習帳
Author And Source
この問題について([cocoa][swift]NSUndoManager), 我々は、より多くの情報をここで見つけました https://qiita.com/m_yukio/items/311afad894c6d67fd4a3著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .