以下是解決方案。
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.
}
}
}
謝謝你的幫助。
你需要做的是添加'tap-gesture' ** action **到'UIView'。請參閱[這裏](https://stackoverflow.com/a/32480721/5175709)。僅供參考有一個非常重要但很容易的概念命名爲:** target-action ** ....目標通常是一個UIVIew ...並且該動作是一旦該視圖被觸發就會觸發的功能。 – Honey
你爲什麼想知道? – Willeke
@ Honey感謝您的信息,但我的問題是針對os x app,而不是針對iOS。 –