【发布时间】:2018-11-04 20:07:12
【问题描述】:
我正在尝试将 undo 和 redo 功能添加到我的 UITextView 实现中。我正在使用attributedText 而不仅仅是UITextView 的text 属性。我已经尝试使用undoManager 中的函数调用,如Apple documentation 中引用的那样,但是似乎没有发生任何事情。我很惊讶我在谷歌上找不到任何关于这个主题的东西。之前有没有人遇到过这个问题/在UITextView 和attributedText 上实现了撤消和重做/知道如何解决这个问题?
示例代码
textView.attributedText = NSMutableAttributedString(string: "SOME TEXT")
@objc func undo(_ sender: UIButton) {
textView.undoManager?.undo()
}
@objc func redo(_ sender: UIButton) {
textView.undoManager?.redo()
}
【问题讨论】:
-
您能否包含代码以显示您在何处注册撤消操作?
-
请确保
IBAction已连接到您的UIButton。我已经测试了您的代码是否适合我。 -
@sanch 是的,这是问题所在,但不确定如何注册所有属性等。@AbecedarioPoint 我接受了您的编辑,但实际上函数是通过编程方式调用的,因此不需要
@IBAction跨度> -
我认为这将回答您的大部分问题。 stackoverflow.com/a/32596899
-
@AbecedarioPoint 您的编辑不正确。程序化 UI 不需要 IBAction,它的字面意思是 InterfaceBuilderAction。 Op 设置了 objc 句柄是正确的,因为 selector 是一个 objc 方法并且类型推断不再隐含在 swift 4 中
标签: swift uitextview nsattributedstring undo redo