UndoManager 撤销 vs 反撤销

class ViewController: UIViewController,UITextViewDelegate {
    
    @IBOutlet weak var textview: UITextView!
    
    fileprivate var curText = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
     
        textview.delegate = self
    }
    
    @objc func updateScore(_ text: String) {
        if curText != text {
            undoManager?.registerUndo(withTarget: self, selector: #selector(updateScore(_:)), object: curText)
        }
        
        curText = text
        
        self.textview.text = curText
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        updateScore(textView.text)
        
        return true
    }
    
    func textViewDidChange(_ textView: UITextView) {
        updateScore(textView.text)
    }
    
    @IBAction func undo(_ sender: Any) {
        self.undoManager?.undo()
    }
    @IBAction func redo(_ sender: Any) {
        self.undoManager?.redo()
    }
}

你可能感兴趣的:(UndoManager 撤销 vs 反撤销)