Swift 4 - NSTextField被选中?

时间:2018-01-29 17:25:27

标签: swift focus nstextfield nsbutton

在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将在选定的文本字段“Button 2 Pressed”上写下

但我还没有找到如何检测选择了哪个NSTextField。有没有人有这样的示例代码/示例?

编辑:

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
}

3 个答案:

答案 0 :(得分:2)

这是我在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"
}

答案 1 :(得分:1)

这应该可以为您提供所需的结果。这是在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
}

答案 2 :(得分:-1)

焦点的文本字段为firstResponder。您可以检查它,然后执行您的代码:

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

(伪代码,在浏览器中输入,未使用Xcode进行测试)