NSTextView event textDidChange

I am doing something similar to:

@IBOutlet var textView: NSTextView!

override func viewDidLoad() {
    super.viewDidLoad()

    textView.delegate = self
}

func textDidChange(notification: NSNotification) {

}

I added "NSTextViewDelegate" at the very beginning.

I want to set the string value of the label if the text in "textView" is changed.

The problem is that my program will simply work without an error message (the breakpoint is in "fun textDidChange () ......"). It will work even if func textDidChange is empty.

Am I doing something wrong?

thank

+4
source share
2 answers

See this answer: textDidChange vs controlTextDidChange

In short, use controlTextDidChange:.

+1
source

Use it, Swift 3

class ViewController: NSViewController, NSTextViewDelegate {

  @IBOutlet var textView: NSTextView!

  override func viewDidLoad() {
    super.viewDidLoad()

    textView.delegate = self

  // NSTextViewDelegate conforms to NSTextDelegate
  func textDidChange(_ notification: Notification) {
    guard let textView = notification.object as? NSTextView else { return }
    print(textView.string)
  }
}

Remember

  • In textDidChange(_ notification: Notification)there
_ Swift 3 Notification NSNotification textView.delegate = self
+3

All Articles