NSTextView结束编辑操作,如NSTextField

the*_*end 4 cocoa nstextview

如何在NSTextView上检测结束编辑操作,就像在NSTextField上一样?我不能将其视为行动或其代表.

Mon*_*olo 14

您可以注册通知,例如NSTextDidEndEditingNotification.

如果要使用委托模式,则应检查NSTextDelegate协议.文档在这里.结束编辑时发送的方法是textDidEndEditing:.

NSTextView是一个子类NSText,所以检查该类的文档也是一个好主意.

NSTextView有一个NSTextViewDelegate属性,您可以用来通知有关更改.委托方法仅仅是获取"结束编辑"通知的便捷方法,例如,与control:textShouldEndEditing您可能知道的不同NSTextField.

class SomeViewController: NSViewController, NSTextViewDelegate {

    var textView: NSTextView!

    func loadView() {

        super.loadView()

        textView.delegate = self
    }

    func textDidBeginEditing(notification: NSNotification) {

        guard let editor = notification.object as? NSTextView else { return }

        // ...
    }

    func textDidEndEditing(notification: NSNotification) {

        guard let editor = notification.object as? NSTextView else { return }

        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)