the Reverend
the Reverend

Reputation: 12549

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.

Upvotes: 5

Views: 3892

Answers (1)

Monolo
Monolo

Reputation: 18253

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 }

        // ...
    }
}

Upvotes: 16

Related Questions