Using Xcode 11/12 I want to create an outlet for a searchField located in the toolbar in the view controller.
Tried control drag to the class for the view controller but that did not work.
I know how to create action outlets for toolbar items but in this case I want to create a NSSearchField outlet in my view controller.
How is this done?
Tried control drag to the class for the view controller but that did not work.
I know how to create action outlets for toolbar items but in this case I want to create a NSSearchField outlet in my view controller.
How is this done?
With the help from another developer I got this all working. Here is the solution:
This assumes the search field is last item.
This assumes the search field is last item.
Code Block override func viewWillAppear() { guard let toolbar = self.view.window?.toolbar else { return } guard let searchFieldToolbarItem = toolbar.items.last else { return } guard let searchField = searchFieldToolbarItem.view as? NSSearchField else { return } searchField.target = self searchField.action = #selector (procSearchFieldInput (sender:)) } @objc func procSearchFieldInput (sender:NSSearchField) { print ("\(#function): \(sender.stringValue)") }