in my NSViewController I have an NSTexField that fires notifications every time its content changes.
I also have several buttons, that have IBActions defined to write characters to the NSTextField. However everytime one of the buttons is pressed the NSTextField gets updated. The notification is not fired.
How can I manually fire a NSControlTextDidChange event?
override func controlTextDidChange(_ notification: Notification) {
if let textField = notification.object as? NSTextField {
filterTable(with: textField.stringValue)
}
}
@IBAction func pressedKeyButton(_ sender: NSButton) {
let character = sender.title
self.searchTextField.stringValue.append(character)
self.searchTextField.textDidChange(Notification(name: .NSControlTextDidChange,object: nil))
}
Since this is a Mac app, you can make this much simpler by using Cocoa Bindings to populate the text field. Simply declare a
dynamicproperty on your view controller with adidSetblock, like so:Then, in Interface Builder, in the Bindings Inspector, bind the text field's
Valuebinding to the view controller, with "Model Key Path" set tosearchString. Also, turn on the "Continuously Updates Value" check box. In your own code, update the text field by altering thesearchStringproperty rather than accessing the text field directly (you might even be able to get rid of the outlet for the text field, as you likely won't need it anymore). Since all changes to the string displayed in the text field will now go through thesearchStringproperty, itsdidSetblock will always be called, and you will be consistently notified.