【问题标题】:NSTextField force becomeFirstResponder removes textNSTextField 强制 becomeFirstResponder 删除文本
【发布时间】: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


【解决方案1】:

删除这行解决了它

self.lblTitle.backgroundColor = Colors.clear
self.lblTitle.borderColor = Colors.clear

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多