I am using NSSearchField for a non-incremental search UI.
The field is configured as follows:
let searchField = NSSearchField()
searchField.sendsSearchStringImmediately = false
searchField.sendsWholeSearchString = true
searchField.target = target
searchField.action = #selector(submit(_:))
My expectation is that the action is sent only when the user explicitly commits the search, such as by pressing Return or clicking the search button.
I understand that clicking the cancel button inside the NSSearchField clears the text and sends the action with an empty string. That behavior is documented and makes sense.
However, I am also seeing the action sent when the user edits the text normally and deletes the last character, causing the search field’s stringValue to become empty. In other words, while the user is just editing the field with the Delete key, the action is sent even though the user did not press Return or explicitly commit the search.
Is this expected behavior for NSSearchField, or is it a bug?
If it is expected, is there a recommended public API to distinguish these cases?
- User pressed Return with a non-empty string
- User clicked the search/cancel button
- User simply edited the text and the string became empty
I would like to keep using NSSearchField’s built-in recent searches behavior, so intercepting Return with control(_:textView:doCommandBy:) is not ideal.
If this is not expected behavior, I will file Feedback with a sample project.
Environment:
- macOS 26.5 (25F71)
- Xcode 26.5 (17F42)