Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select next NSTextField with Tab key in Swift

Is there a way to change the responder or select another text view by pressing tab on the keyboard, in Swift?

enter image description here

Notes: It's for a fill in the blank type application.

My VC creates a list of Words [Word], and each of those words has its own WordView - word.wordView. The WordView is what is displayed. WordView is a child of NSTextView.

I tried to override keydown but it doesn't allow me to type anything in the text view.

like image 282
Aaron Avatar asked Mar 15 '16 00:03

Aaron


2 Answers

You have to connect your textField nextKeyView to the next textField through the IB or programmatically:

textField1.nextKeyView = textField2

enter image description here

like image 145
Leo Dabus Avatar answered Oct 04 '22 22:10

Leo Dabus


Assuming you want to go from textView1 to textView2. First set the delegate:

self.textView1.delegate = self

Then implement the delegate method:

func textView(textView: NSTextView, doCommandBySelector commandSelector: Selector) -> Bool {
    if commandSelector == "insertTab:" && textView == self.textView1 {
        self.window.makeFirstResponder(self.textView2)
        return true
    }
    return false
}
like image 31
rocky Avatar answered Oct 05 '22 00:10

rocky