Select 下一个带 Tab 键的 NSTextField Swift
Select next NSTextField with Tab key in Swift
在 Swift 中,是否可以通过按键盘上的 Tab 键来更改响应者或 select 另一个文本视图?
备注:
这是一个填空类型的应用程序。
我的 VC 创建了一个单词列表 [Word],每个单词都有自己的 WordView - word.wordView。显示的是 WordView。 WordView 是 NSTextView 的子项。
我试图覆盖 keydown 但它不允许我在文本视图中键入任何内容。
假设您想从 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
}
您必须通过 IB 或以编程方式将您的 textField nextKeyView 连接到下一个 textField:
textField1.nextKeyView = textField2
如果你想控制你的字段如何在 Swift 中的字段之间使用箭头键切换或移动,你可以将它添加到你的委托中以及一些移动有意义的代码来执行实际的移动,如下一步移动通过在 superview 上找到控件,该控件明显显示在控件的下方或右侧,并且可以接受焦点。
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
}
我遇到了相同或类似的问题,因为我想使用 NSTextView 字段以允许输入多行文本,但在这种字段中输入制表符毫无意义.我找到了一个简单的解决方法:NSTextView 有一个 isFieldEditor
的实例 属性,默认设置为 false
;只需将其设置为 true
,标签现在将跳到下一个字段。
在 Swift 中,是否可以通过按键盘上的 Tab 键来更改响应者或 select 另一个文本视图?
备注: 这是一个填空类型的应用程序。
我的 VC 创建了一个单词列表 [Word],每个单词都有自己的 WordView - word.wordView。显示的是 WordView。 WordView 是 NSTextView 的子项。
我试图覆盖 keydown 但它不允许我在文本视图中键入任何内容。
假设您想从 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
}
您必须通过 IB 或以编程方式将您的 textField nextKeyView 连接到下一个 textField:
textField1.nextKeyView = textField2
如果你想控制你的字段如何在 Swift 中的字段之间使用箭头键切换或移动,你可以将它添加到你的委托中以及一些移动有意义的代码来执行实际的移动,如下一步移动通过在 superview 上找到控件,该控件明显显示在控件的下方或右侧,并且可以接受焦点。
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
}
我遇到了相同或类似的问题,因为我想使用 NSTextView 字段以允许输入多行文本,但在这种字段中输入制表符毫无意义.我找到了一个简单的解决方法:NSTextView 有一个 isFieldEditor
的实例 属性,默认设置为 false
;只需将其设置为 true
,标签现在将跳到下一个字段。