如何在NSSearchField中更改cancelbutton的行为?

时间:2013-01-30 07:11:42

标签: macos cocoa nssearchfield

在Finder中,我发现当NSSearchfield第一次获得焦点时取消按钮消失,但是当我向搜索域输入内容时,取消按钮始终存在,除非我点击它。我还创建了一个nssearchfield manully和输入一些单词。但是,当我一个接一个地删除单词时,最后,取消按钮消失。它看起来像nssearchfield的默认行为,那么我该如何更改默认行为? 请帮忙,谢谢!

1 个答案:

答案 0 :(得分:2)

您可以直接访问取消按钮:

夫特

let searchField = NSSearchField()

// Change the button:
if let cell = searchField.cell as? NSSearchFieldCell,
    let cancelButton = cell.cancelButtonCell {
    // Here you can access all properties of a NSButtonCell
}

// Hide the button:
searchField.cell?.cancelButtonCell = nil

目标C

NSSearchField* searchField = [NSSearchField new]

// Change the button:
NSSearchFieldCell* cell = searchField.cell
NSButtonCell* cancelButton = cell.cancelButtonCell
if (cancelButton) {
    // Here you can access all properties of a NSButtonCell
}

// Hide the button:
searchField.cell.cancelButtonCell = nil