【发布时间】:2018-08-12 06:14:13
【问题描述】:
在自定义 NSTableCellView 上使用 NSTextField。
当用户按下单元格上的任意位置时,我希望NSTextField 成为第一响应者。
当我这样做时,文本消失了,用户需要按任意键盘键才能返回,如何禁用此行为?
代码:
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
if (event.clickCount == 2) {
self.beginEditing()
} else {
self.endEditing()
}
}
private func beginEditing() {
self.lblTitle.isEditable = true
self.window?.makeFirstResponder(self.lblTitle)
self.lblTitle.backgroundColor = Colors.clear
self.lblTitle.borderColor = Colors.clear
}
private func endEditing() {
self.lblTitle.isEditable = false
self.lblTitle.backgroundColor = Colors.red
}
【问题讨论】:
-
请阅读
becomeFirstResponder文档的讨论部分。 -
@Willeke 对不起,这段代码是经过多次尝试的。我确实在第一响应者之前尝试了窗口,但没有运气。更新答案
标签: swift xcode macos cocoa nstextfield