我正在代码中实现撤消/重做功能,但由于某种原因,我的重做功能无法正常工作,这是我的文本视图代码:
func textViewDidBeginEditing(_ textView: UITextView) {
if descBox.textColor == .lightGray {
DescriptionCell.descPlaceholder = descBox.text
let descText = descBox.text
undoManager?.registerUndo(withTarget: self, handler: {
(targetSelf) in
targetSelf.descBox.text = descText
targetSelf.descBox.textColor = .lightGray
})
descBox.text = nil
descBox.textColor = .black
}
}
func textViewDidEndEditing(_ textView: UITextView) {
if descBox.text.isEmpty {
descBox.text = DescriptionCell.descPlaceholder
descBox.textColor = .lightGray
}
let descText = descBox.text
undoManager?.registerUndo(withTarget: self, handler: {
(targetSelf) in
targetSelf.descBox.text = descText
})
}
然后我将其用于ViewController工具栏:
@objc func Undo() {
undoManager?.undo()
}
@objc func Redo() {
undoManager?.redo()
}
然后在viewDidLoad中:
let undoKeyboard = UIBarButtonItem(image: UIImage(named: "Image-2"), style: .plain, target: self, action: #selector(Undo))
undoKeyboard.tintColor = .lightGray
let redoKeyboard = UIBarButtonItem(image: UIImage(named: "Image-1"), style: .plain, target: self, action: #selector(Redo))
redoKeyboard.tintColor = .green