在快速(4.2)中,我无法重做然后在最初撤消后撤消。在重做和随后的撤消操作期间,永远不会调用UndoManager.registerUndo(...)中的选择器
我已经在项目和游乐场中尝试了无尽变化。
import Cocoa
class UndoResponder: NSObject {
@objc func printSender(_ sender: Any) {
print("printSender: ", sender)
}
}
var undoResponder = UndoResponder()
var undoManager = UndoManager()
undoManager.registerUndo(withTarget: undoResponder, selector:
#selector(UndoResponder.printSender(_:)), object: "Hi!")
print("about to undo")
undoManager.undo()
print("about to redo")
undoManager.redo()
print("about to undo")
undoManager.undo()
// output:
// about to undo
// printSender: Hi!
// about to redo
// about to undo
预期输出:
// about to undo
// printSender: Hi!
// about to redo
// printSender: Hi!
// about to undo
// printSender: Hi!
答案 0 :(得分:1)
正如问题评论中所指出的,我没有注册重做。所以我替换为以下内容:
import Cocoa
class Undoable {
var undoManager = UndoManager()
var object: Any = "None"
@objc func setObject(_ newObject: Any) {
let oldObject = object
object = newObject
undoManager.registerUndo(withTarget: self, selector:
#selector(Undoable.setObject(_:)), object: oldObject)
}
func undo() {
undoManager.undo()
print("after undo: ", object)
}
func redo() {
undoManager.redo()
print("after redo: ", object)
}
}
let undoable = Undoable()
print("Initial: ", undoable.object)
undoable.setObject(1)
print("After set: ", undoable.object)
undoable.undo()
undoable.redo()
undoable.undo()
输出:
Initial: (None)
After set: 1
after undo: (None)
after redo: 1
after undo: (None)