[在Swift中使用Tab键选择下一个NSTextField

问题描述 投票:13回答:3

是否可以通过在Swift中按键盘上的Tab键来更改响应者或选择其他文本视图?

enter image description here

注意:用于填写空白类型的应用程序。

[我的VC创建了单词[Word]的列表,并且每个单词都有其自己的WordView-word.wordView。 WordView将显示。 WordView是NSTextView的子级。

我试图覆盖按键,但是不允许我在文本视图中键入任何内容。

swift xcode macos keydown nstextfield
3个回答
15
投票

您必须通过IB或以编程方式将textField nextKeyView连接到下一个textField:

textField1.nextKeyView = textField2

enter image description here


6
投票

假设您要从textView1转到textView2。首先设置代表:

self.textView1.delegate = self

然后实现委托方法:

func textView(textView: NSTextView, doCommandBySelector commandSelector: Selector) -> Bool {
    if commandSelector == "insertTab:" && textView == self.textView1 {
        self.window.makeFirstResponder(self.textView2)
        return true
    }
    return false
}

0
投票

[如果您想对字段如何在Swift中的字段之间进行制表或移动并使用箭头键进行控制,可以将其与有意义的移动代码一起添加到您的委托中,以通过在超级视图上找到控件来进行实际的移动,例如下一步移动。可见地显示在控件的下方或右边,并且可以接受焦点。

 public func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
        switch commandSelector {
        case #selector(NSResponder.insertTab(_:)), #selector(NSResponder.moveDown(_:)):
            // Move to the next field
            Swift.print("Move next")
            return true
        case #selector(NSResponder.moveUp(_:)):
            // Move to the previous field
            Swift.print("Move previous")
            return true
        default:
            return false
        }
        return false // I didn't do anything
    }
© www.soinside.com 2019 - 2024. All rights reserved.