I want to know why is the NSSearchField action is called when the sidebar selection has changed? Without changing the search term or even clearing it.
This interferes with the controller logic because I need to load the correct view controller as per sidebar selection. The NSSearchField action load the incorrect view controller. If I don't change the sidebar selection, then it is working as intended. But when I do, it interferes. It is quite annoying.
I keep a reference to the NSSearchField here.
class WindowController: NSWindowController {
var searchField: NSSearchField?
//...
}
Adding the action for NSSearchField is done here.
extension WindowController: NSToolbarDelegate {
func toolbarWillAddItem(_ notification: Notification) {
guard let item = notification.userInfo?["item"] as? NSToolbarItem else { return }
if item.itemIdentifier == .searchToolbarItem, let searchToolbarItem = item as? NSSearchToolbarItem {
searchToolbarItem.searchField.delegate = self
searchToolbarItem.searchField.target = self
searchToolbarItem.searchField.action = #selector(didStartSearching(_:))
searchField = searchToolbarItem.searchField
}
}
}
The method for the NSSearchField action is here. The actual implementation is not important:
@IBAction func didStartSearching(_ sender: NSSearchField) {
print(“This method is called!”)
//...
}
The sidebar's delegate is called in here.
extension WindowController: SidebarViewControllerDelegate {
func sidebarSelectionDidChange(_ sidebarViewController: SidebarViewController, somethingToPass: ModelData) {
print("Sidebar Has Changed Selection!")
//...
}
}