TalkingCode
TalkingCode

Reputation: 13567

Cocoa: NextKeyView - Tab Order

in my cocoa application I want to change the tab order of the controls so I can switch from a textfield to a button. I did connect the nextKeyView property of the textfield with the button but it seems to have no effect at all.

I already activated the appropriate system settings for Keyboard so I can tab to all controls now. It works fine but not in the order I set as nextKeyView.

Upvotes: 3

Views: 1927

Answers (2)

drootang
drootang

Reputation: 2503

The answer provided by @sach solved this for me.

I created a new storyboard project, and added 3 text fields. I set a custom order by linking them together with nextKeyView in Interface Builder, but this had no effect at runtime. This is the simplest test case that I could come up with to replicate your (and my) problem.

To solve it using @sach's answer, create a new outlet and connect it to one of the NSTextFields:

@IBOutlet var firstTextField: NSTextField!

In the ViewController, I then added:

override func viewWillAppear() {
    // Must execute this after the window is connected
    self.view.window!.initialFirstResponder = nameField
}

Re-run and the text fields cycle properly according to the order set using the nextKeyView links

Upvotes: 3

sach
sach

Reputation: 1069

You need to connect your window's initialFirstResponder outlet to at least 1 field. Some controls like NSTabView have one more initialResponder. So if you have anything inside tabview you need to connect them also. Overall logic is you need to connect all initialresponde to atleast 1 field

Upvotes: 2

Related Questions