2017-06-06 101 views
0

如何檢查我是否按下了NSSearchField搜索按鈕? search button of NSSearchField如何檢查NSSearchField的搜索按鈕1是否被按下

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

什麼,我需要做的,而不是searchButtonIsClicked

+0

你需要做的是添加'tap-gesture' ** action **到'UIView'。請參閱[這裏](https://stackoverflow.com/a/32480721/5175709)。僅供參考有一個非常重要但很容易的概念命名爲:** target-action ** ....目標通常是一個UIVIew ...並且該動作是一旦該視圖被觸發就會觸發的功能。 – Honey

+0

你爲什麼想知道? – Willeke

+0

@ Honey感謝您的信息,但我的問題是針對os x app,而不是針對iOS。 –

回答

0

以下是解決方案。

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. 
     } 
    } 
} 

謝謝你的幫助。

+0

這不是問題的答案。或者是「如何更改菜單?」這個問題? – Willeke

相關問題