0

I have a simple view with 4 textfields tf1 - tf4.

I want to set the initial focus on tf2, that works fine.

Then I want, that tf1 gets focus, then tf2 again - that does not work, the focus cycles through all 4 textfields.

I tried like this:

override func viewWillAppear()
{ super.viewWillAppear()
  tf2.becomeFirstResponder()

  tf2.nextKeyView = tf1
  tf1.nextKeyView = tf2
}

Where is my mistake?

mica
  • 2,872
  • 3
  • 22
  • 47
  • 1
    Don't call `becomeFirstResponder`. "Use the NSWindow makeFirstResponder: method, not this method, to make an object the first responder. Never invoke this method directly." – Willeke Mar 23 '17 at 14:30
  • 1
    Set `initialFirstResponder` of the window to `tf2`. – Willeke Mar 23 '17 at 15:11
  • what is the best place to set the NSWindows firstResponder? Is also viewWillAppear OK? How can I get the window in the viewcontroller? – mica Mar 24 '17 at 10:31
  • Thanks!!
    With: self.view.window!.initialFirstResponder = tf2 it works fine
    – mica Mar 24 '17 at 16:26

1 Answers1

0

thanks @Willeke:

It works fine with:

override func viewWillAppear()
{ super.viewWillAppear()
  tf2.nextKeyView = tf1
  tf1.nextKeyView = tf2      
  self.view.window!.initialFirstResponder = tf2
}
mica
  • 2,872
  • 3
  • 22
  • 47