如何在Swift中注册NSUndoManager? [英] How do I register NSUndoManager in Swift?
问题描述
如何在Swift中使用 NSUndoManager
?
How do I use NSUndoManager
in Swift?
这是我试过的一个Objective-C示例复制:
Here's an Objective-C example I've tried to replicate:
[[undoManager prepareWithInvocationTarget:self] myArgumentlessMethod];
然而,Swift似乎没有 NSInvocation
,(貌似)意味着我无法调用它未实现的 undoManager
上的方法。
Swift, however, seems to not have NSInvocation
, which (seemingly) means I can't call methods on the undoManager
that it doesn't implement.
我在Swift中尝试过基于对象的版本,但它似乎让我的Playground崩溃了:
I've tried the object-based version in Swift, but it seems to crash my Playground:
undoManager.registerUndoWithTarget(self, selector: Selector("myMethod"), object: nil)
然而它似乎崩溃,甚至with my object接受类型为 AnyObject的参数?
However it seems to crash, even with my object accepts an argument of type AnyObject?
在Swift中执行此操作的最佳方法是什么?有没有办法避免使用基于对象的注册发送不必要的对象?
What's the best way to do this in Swift? Is there a way to avoid sending an unnecessary object with the object-based registration?
推荐答案
我在游乐场试过这个并且它完美无缺:
I tried this in a Playground and it works flawlessly:
class UndoResponder: NSObject {
func myMethod() {
println("Undone")
}
}
var undoResponder = UndoResponder()
var undoManager = NSUndoManager()
undoManager.registerUndoWithTarget(undoResponder, selector: Selector("myMethod"), object: nil)
undoManager.undo()
这篇关于如何在Swift中注册NSUndoManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!