带有NSSearchField搜索结果的上下文菜单

时间:2016-02-29 16:29:43

标签: swift macos cocoa contextmenu nssearchfield

我希望有一个上下文菜单,可以在搜索字段中输入文本时显示搜索结果。这是个 OS X中默认邮件应用的image执行此操作。我知道如何根据用户的搜索请求过滤字符串数组,但我不知道如何以这种方式显示它。我正在使用Swift和Cocoa应用程序。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:5)

根据上一个答案构建,这是一个简单的Swift 3类,您可以使用它来自动处理最近的搜索。您可以在故事板中将其添加为自定义类,也可以直接添加。 它看起来像这样:

enter image description here

import Cocoa

class RecentMenuSearchField: NSSearchField {

    lazy var searchesMenu: NSMenu = {

        let menu = NSMenu(title: "Recents")

        let recentTitleItem = menu.addItem(withTitle: "Recent Searches", action: nil, keyEquivalent: "")
        recentTitleItem.tag = Int(NSSearchFieldRecentsTitleMenuItemTag)

        let placeholder = menu.addItem(withTitle: "Item", action: nil, keyEquivalent: "")
        placeholder.tag = Int(NSSearchFieldRecentsMenuItemTag)

        menu.addItem( NSMenuItem.separator() )

        let clearItem = menu.addItem(withTitle: "Clear Menu", action: nil, keyEquivalent: "")
        clearItem.tag = Int(NSSearchFieldClearRecentsMenuItemTag)

        let emptyItem = menu.addItem(withTitle: "No Recent Searches", action: nil, keyEquivalent: "")
        emptyItem.tag = Int(NSSearchFieldNoRecentsMenuItemTag)

        return menu
    }()

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }

    //create menu
    private func initialize() {
        self.searchMenuTemplate = searchesMenu
    }
}

答案 1 :(得分:1)

NSSearchField searchMenuTemplate(NSMenu)包含菜单项(NSMenuItem),搜索字段使用特定标记来填充菜单。

这里的recentSearches数组只是传递最近搜索字符串补充中使用的其他String而不是必需的(我认为它是存储最近的搜索但是没有)。当用户清除最近搜索时,NSSearchField也清除此数组。

您还可以使用类别配置菜单,此处提供更多信息: Configuring a Search Menu — Apple Developer

示例:

@IBOutlet weak var search: NSSearchField!

/// Array of string containing additional recents search (custom search)
var recentSearches = [String]()

/// Search Field Recents Menu
lazy var searchesMenu: NSMenu = {

    let menu = NSMenu(title: "Recents")

    let i1 = menu.addItem(withTitle: "Recents Search", action: nil, keyEquivalent: "")
    i1.tag = Int(NSSearchFieldRecentsTitleMenuItemTag)

    let i2 = menu.addItem(withTitle: "Item", action: nil, keyEquivalent: "")
    i2.tag = Int(NSSearchFieldRecentsMenuItemTag)

    let i3 = menu.addItem(withTitle: "Clear", action: nil, keyEquivalent: "")
    i3.tag = Int(NSSearchFieldClearRecentsMenuItemTag)

    let i4 = menu.addItem(withTitle: "No Recent Search", action: nil, keyEquivalent: "")
    i4.tag = Int(NSSearchFieldNoRecentsMenuItemTag)

    return menu
}()

override func viewDidLoad() {
    super.viewDidLoad()
    recentSearches = ["Toto","Titi","Tata"]
    search.delegate = self
    search.recentSearches = recentSearches
    search.searchMenuTemplate = searchesMenu
}

答案 2 :(得分:0)

您需要创建一个带有占位符的特殊标记的NSMenu项目,以便搜索字段知道放置最近项目的位置,清除操作等。

查看searchMenuTemplate的文档,以及Tags:NSSearchFieldRecentsMenuItemTag等。

基本上,在IB中制作上下文菜单。拖动搜索字段以将该菜单用作searchMenuTemplate,然后使用所需的标记填充菜单项以获取清晰,最近的项目等。