I want a function to fire every time the user makes a change to my NSTextView. I got this to work in an iOS app and am now trying to make it work in an OS X app. I created an outlet for my NSTextView and wrote the following Swift code:
import Cocoa
class ViewController: NSViewController, NSTextViewDelegate {
@IBOutlet var textViewOutlet: NSTextView!
func textViewDidChange(textView: NSTextView) {
print("Text view changed!")
}
}
I don't get any errors but my statement doesn't print.
It should be textDidChange(notification:). Try like this:
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
print(textView.string)
}
This should be why there is no delegate assignment.
TextViewOutlet.delegate = self;
And the method actually looks like this. Notice the parameters
func textDidChange(_ notification: Notification) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With