16

Is there a way to change the responder or select another textfield 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 NSTextField.

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

4 Answers 4

21
+50

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

textField1.nextKeyView = textField2

enter image description here

9
  • Can't quite make this work, but it sounds like a great idea. My loop currently looks like this: previousWord.wordView.nextKeyView = word.wordView
    – Aaron
    Mar 24, 2016 at 19:29
  • 1
    Found the problem. I am using NSTextView not NSTextField. NSTextView just inserts a tab, while NSTextField moves to the next TextField. Interesting.
    – Aaron
    Mar 24, 2016 at 19:39
  • 1
    Thanks so much @Leo Dabus That was exactly the help I needed!! Been stumped on this problem for weeks.
    – Aaron
    Mar 24, 2016 at 19:46
  • 1
    @Aaron you can set the textField delegate and add textField method controlTextDidChange to monitor the user text input and decide where to go from there. You would probably need to store that info somewhere in memory or disk.
    – Leo Dabus
    Mar 24, 2016 at 22:50
  • 1
    It works perfectly! Thanks again @Leo Dabus for all your help! The project file helped a lot too!
    – Aaron
    Mar 25, 2016 at 1:15
5

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
}
10
  • Help me understand what "self" is in this case? the View Controller?
    – Aaron
    Mar 18, 2016 at 0:23
  • 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.
    – Aaron
    Mar 18, 2016 at 0:25
  • When I put the func in the VC it says: ViewController does not have member 'window'
    – Aaron
    Mar 18, 2016 at 1:23
  • 'window' is the window that your view lives in. self is whatever it is that owns the NSTextViews. It is usually in a view controller but not necessarily.
    – rocky
    Mar 18, 2016 at 4:43
  • Tried: self.window = self.view.window! It doesn't work, returns nil. Ideas on how to reference the VC's window?
    – Aaron
    Mar 18, 2016 at 18:27
1

I had the same or a similar problem, in that I wanted to use an NSTextView field, to allow multiple lines of text to be entered, but it was the sort of field where entering a tab character would make no sense. I found an easy fix for this: NSTextView has an instance property of isFieldEditor, which is set to false by default; simply set this to true, and tabs will now skip to the next field.

0

If you want some control over how your field tabs or moves with arrow keys between fields in Swift, you can add this to your delegate along with some move meaningful code to do the actual moving like move next by finding the control on the superview visibly displayed below or just to the right of the control and can accept focus.

 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
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.