美文网首页
UndoManager 撤销 vs 反撤销

UndoManager 撤销 vs 反撤销

作者: 小凡凡520 | 来源:发表于2018-10-30 22:53 被阅读38次
    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 反撤销

          本文链接:https://www.haomeiwen.com/subject/ayygtqtx.html