Search code examples
swiftxcodemacoscocoanstextfield

NSTextField force becomeFirstResponder removes text


Using NSTextField on Custom NSTableCellView.

When the user presses anywhere on the cell, I would like NSTextField to become the first responder. When I do, the text disappears, and the user needs to press any keyboard key for it to come back, how to disable this behavior?

Code:

    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
    }

Solution

  • Removing this lines solved it

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