Search code examples
xcodemacosinterface-builderosx-elcapitan

First responder is set right on OSX10.7 but not in OSX 10.10


There is a NSWindow that is 'constructed' using NSViews. I'm interested in one of those NSViews. This view has NSButtons and a NSTextfield.

On OSX 10.7, the NSTextfield gets the focus as soon as the app opens, which is what I want. On 10.11, one of the NSButtons gets the focus (B1), instead of the NSTextfield. How may I fix this behavior?.

The NSView I'm interested in looks like this:

B1   B2   B3   NSTextField B4   V1   V2
1    2    3    4           5    6    7

The first line "shows" the NSButtons (BX, X=1..n), the NSTextField and few other Views (VX).
The second line "shows" the tag value for each of those elements.

What have I tried so far?
1. Switched the tag value of B1 with the NSTextField.
2. Connected the nextKeyView outlet of each element in the desired order.
3. The NSView also has a nextKeyView outlet. I connected it to the NSTextfield.

Nothing worked.


Solution

  • Having buttons getting focused is a matter of global system settings, not a problem in your program. The shortcut to change it is defined in System Preferences / Keyboard / Shortcuts / Keyboard / "Change the way Tab moves focus", which is Ctrl+F7 by default. Note that you might have to press Ctrl+Fn+F7 depending on keyboard configuration.

    Anyway, in case you need to ensure the window pops up with a particular control focused, it is controlled by NSWindow's initialFirstResponder property. Tab View items have that property too. And if the window is already visible, call makeFirstResponder: method to explicitly move the focus – but mind that it may fail e.g. because of input validation.