Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView end editing action like NSTextField

How can I detect an end editing action on a NSTextView, just like on NSTextField ? I can not see it as an action or in its delegate.

like image 701
the Reverend Avatar asked Dec 07 '22 12:12

the Reverend


1 Answers

You can register for notifications, such as NSTextDidEndEditingNotification.

If you want to use the delegate pattern, then you should check the NSTextDelegate protocol. Docs here. The method sent on end editing is textDidEndEditing:.

NSTextView is a subclass of NSText, so it is a good idea to check the docs for that class, too.

Example

NSTextView has a NSTextViewDelegate property you can use to be notified about changes. The delegate methods are mere convenience methods to obtain the "end editing" notification, unlike control:textShouldEndEditing you may know from NSTextField, for example.

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 }

        // ...
    }
}
like image 97
Monolo Avatar answered Jan 23 '23 20:01

Monolo