有没有办法在Swift中按键盘上的标签来更改响应者或选择其他文本视图?
注意: 这是填写空白类型的应用程序。
我的VC创建了一个单词[Word]列表,每个单词都有自己的WordView - word.wordView。 WordView是显示的内容。 WordView是NSTextView的孩子。
我试图覆盖keydown,但它不允许我在文本视图中输入任何内容。
答案 0 :(得分:9)
答案 1 :(得分:5)
假设你想从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
}
答案 2 :(得分: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
}