1

How do I check if I have pressed on an NSSearchField search button? search button of NSSearchField

@IBAction func searchTextField(_ sender: NSSearchField) {
        if `searchButtonIsClicked`  {
            //Code if searchButtonIsClicked
            return
        }
        if sender.stringValue != ""  {
            //My code
        }
    }

What I need to do instead of searchButtonIsClicked?

  • What you need to do is add `tap-gesture` **action** to that `UIView`. See [here](https://stackoverflow.com/a/32480721/5175709). FYI there is a very important but easy concept named: **target-action**....The target is usually a UIVIew...and the action is a function that would be triggered once the view has been tapped. – mfaani Jun 06 '17 at 20:31
  • Why do you want to know? – Willeke Jun 06 '17 at 20:56
  • @ Honey Thank you for your information, but my question is for os x app and not for iOS. – C. Piersigilli Jun 07 '17 at 19:02
  • @Willeke I want to check if I click in search field or search button or cancel button in the **NSSearchField**, because '@IBAction func searchTextField(_ sender: NSSearchField)' doesn't recognize that two actions. – C. Piersigilli Jun 07 '17 at 19:09
  • Do you want to change the functionality of the buttons? – Willeke Jun 07 '17 at 20:51

2 Answers2

0

Here is the solution.

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var searchFieldText: NSSearchField!
    @IBOutlet weak var labelSearchText: NSTextField!

    @IBAction func searchFieldButton(_ sender: NSSearchField) {
        if searchFieldText.stringValue != "" {
            //My code. For example:
            labelSearchText.stringValue = searchFieldText.stringValue
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let cellMenu = NSMenu(title: "Search Menu")

        var item: NSMenuItem!
        item = NSMenuItem(title: "Clear", action: nil, keyEquivalent: "")
        item.tag = Int(NSSearchFieldClearRecentsMenuItemTag)
        cellMenu.insertItem(item, at: 0)

        item = NSMenuItem.separator()
        item.tag = Int(NSSearchFieldRecentsTitleMenuItemTag)
        cellMenu.insertItem(item, at: 1)

        item = NSMenuItem(title: "Recent Searches", action: nil, keyEquivalent: "")
        item.tag = Int(NSSearchFieldRecentsTitleMenuItemTag)
        cellMenu.insertItem(item, at: 2)

        item = NSMenuItem(title: "Recents", action: nil, keyEquivalent: "")
        item.tag = Int(NSSearchFieldRecentsMenuItemTag)
        cellMenu.insertItem(item, at: 3)
        searchFieldText.searchMenuTemplate = cellMenu
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
}

Thank you for your help.

0

I think this demo can resolve your question.

Swift Code is below:

if let cell = searchField.cell as? NSSearchFieldCell {
        let searchButtonCell: NSButtonCell = cell.searchButtonCell!
        let cacelButtonCell: NSButtonCell = cell.cancelButtonCell!
        searchButtonCell.target = self
        cacelButtonCell.target = self
        searchButtonCell.action = #selector(clickSearchButton(_:))
        cacelButtonCell.action = #selector(clickCacelButton(_:))
    }
zhaowweny
  • 241
  • 1
  • 2
  • 6