Swift 4 - 已选择 NSTextField?

Swift 4 - NSTextField Selected?

在 OSX 应用程序上我有 3 NSTextField 和 2 NSButtons

@IBOutlet weak var TF1: NSTextField!
@IBOutlet weak var TF2: NSTextField!
@IBOutlet weak var TF3: NSTextField!
@IBOutlet weak var Bt1: NSButton!
@IBOutlet weak var Bt2: NSButton!

Bt1 将在选定的文本字段“按下按钮 1”上书写
Bt2 将在选定的文本字段上写入“按下按钮 2”

但我还没有找到如何检测选择了哪个 NSTextField。有人有这方面的样本 code/example 吗?

编辑:

2 个解决方案完美运行
感谢@MwcsMac 和@sfjac

@IBAction func test(_ sender: NSButton) {
    var textField : NSTextField? = nil
    // the focused control is the first responder of the window
    let responder = sender.window?.firstResponder
    // if a text field is focused, the first responder is the field editor, a NSTextView
   if let textView = (responder as? NSTextView) {
        if textView.isFieldEditor {
            // the focused text field is the delegate of the field editor
            textField = textView.delegate as? NSTextField
        }
    }
    else {
        if responder is NSTextField {
            textField = responder as? NSTextField
        }
    }
    textField?.stringValue = "test"
}

    @IBAction func Bt1Pressed(_ sender: NSButton) {
    if isTextField(inFocus: TF1){
        TF1.stringValue = "B1 Pressed"
    }
    if isTextField(inFocus: TF2){
        TF2.stringValue = "B1 Pressed"
    }
    if isTextField(inFocus: TF3){
        TF3.stringValue = "B1 Pressed"
    }
}

@IBAction func Bt2Pressed(_ sender: NSButton) {
    if isTextField(inFocus: TF1) {
        TF1.stringValue = "B2 Pressed"
    }
    if isTextField(inFocus: TF2){
        TF2.stringValue = "B2 Pressed"
    }
    if isTextField(inFocus: TF3){
        TF3.stringValue = "B2 Pressed"
    }
}

func isTextField(inFocus textField: NSTextField) -> Bool {
    var inFocus = false
    inFocus = (textField.window?.firstResponder is NSTextView) && textField.window?.fieldEditor(false, for: nil) != nil && textField.isEqual(to: (textField.window?.firstResponder as? NSTextView)?.delegate)
    return inFocus
}

具有焦点的文本字段将是 firstResponder。您可以检查一下,然后执行您的代码:

if myNSWindow.firstResponder == TF1 {
  // Do something with TF1
}

(伪代码,在浏览器中输入,未经 Xcode 测试)

这应该会给您想要的结果。这在 Xcode 9.2 和 macOS 10.13.3 上进行了测试。

@IBAction func Bt1Pressed(_ sender: NSButton) {
    if isTextField(inFocus: TF1){
        TF1.stringValue = "B1 Pressed"
    }
    if isTextField(inFocus: TF2){
        TF2.stringValue = "B1 Pressed"
    }
    if isTextField(inFocus: TF3){
        TF3.stringValue = "B1 Pressed"
    }
}

@IBAction func Bt2Pressed(_ sender: NSButton) {
    if isTextField(inFocus: TF1) {
        TF1.stringValue = "B2 Pressed"
    }
    if isTextField(inFocus: TF2){
        TF2.stringValue = "B2 Pressed"
    }
    if isTextField(inFocus: TF3){
        TF3.stringValue = "B2 Pressed"
    }
}

func isTextField(inFocus textField: NSTextField) -> Bool {
    var inFocus = false
    inFocus = (textField.window?.firstResponder is NSTextView) && textField.window?.fieldEditor(false, for: nil) != nil && textField.isEqual(to: (textField.window?.firstResponder as? NSTextView)?.delegate)
    return inFocus
}

这是我在 Objective-C 中所做的:

id responder = [window firstResponder];
if (responder && [responder isKindOfClass:[NSTextView class]] && [responder isFieldEditor])
    responder = [responder delegate];
if ([responder isKindOfClass:[NSTextField class]])
    [responder setStringValue:@"test"];

和我(可能很笨拙)对 Swift 的翻译:

@IBAction func test(_ sender: NSButton) {
    var textField : NSTextField? = nil
    // the focused control is the first responder of the window
    let responder = sender.window?.firstResponder
    // if a text field is focused, the first responder is the field editor, a NSTextView
    if let textView = (responder as? NSTextView) {
        if textView.isFieldEditor {
            // the focused text field is the delegate of the field editor
            textField = textView.delegate as? NSTextField
        }
    }
    else {
        if responder is NSTextField {
            textField = responder as? NSTextField
        }
    }
    textField?.stringValue = "test"
}