macoscocoanssearchfield

Can't exit NSSearchField after inserted text and cleared it


I added an NSSearchField to my NSViewController, which is the main controller of my outline view. Now it implements next classes:

class MainCatalogNSViewController: NSViewController, NSOutlineViewDelegate, NSOutlineViewDataSource, NSSearchFieldDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        searchField.delegate = self
    }
}

The search field works well without any issues until I set a delegate as above. Once it's set, I can't exit it if was edited and cleared.

Could you please explain to me why this happens? Can this issue be related to outline view? I used the NSSearchField before, and it always worked well.

enter image description here


Solution

  • The root of the issue was the next method, declared in the NSViewController, which prevents saving of the empty cells in the outline view:

    func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
        // don't allow empty node names
        return !(fieldEditor.string.isEmpty ?? true)
    }
    

    And now it handles the NSSearchField events in this case, instead of outline view table cells editing.