NSTextView结束编辑行为类似于NSTextField

5
如何在NSTextView上检测编辑结束的操作,就像在NSTextField上一样?我无法将其视为Action或其Delegate中的内容。
1个回答

16

你可以注册通知,例如NSTextDidEndEditingNotification

如果您想使用代理模式,则应检查NSTextDelegate协议。文档在这里。结束编辑时发送的方法是textDidEndEditing:

NSTextViewNSText的子类,因此最好也检查该类的文档

示例

NSTextView有一个NSTextViewDelegate属性,您可以使用它来接收更改的通知。与NSTextField中的control:textShouldEndEditing不同,这些委托方法仅是方便的方法来获取“结束编辑”通知。

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 }

        // ...
    }
}

你能举个例子吗? - Aviram Netanel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接