I tried to register a delegate for a NSViewController like this:
class ViewController: NSViewController , NSTextFieldDelegate{
override func viewDidLoad() {
super.viewDidLoad()
textfield.delegate = self
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@IBOutlet weak var textfield: NSTextField!
func textDidChange(_ notification: Notification)
{ print ("text did change ")
}
}
the func textDidChange is not called, if the textfield changes its content.
In the Storyboard I made the following connections:
textDidChange
isn't part of NSTextFieldDelegate
, it's part of NSTextDelegate
, and it's typically used for NSTextView
s, not NSTextField
s.
NSTextFieldDelegate
adopts NSControlTextEditingDelegate
, including controlTextDidChange
, which is what you're looking for.
override func controlTextDidChange(_ obj: Notification) {
print( "text did change" )
}
Note that you can set the delegate either via Storyboard outlet, or via code, but you don't need to do both (I prefer the former, but it's totally subjective which is superior).