0

[MacOS Mojave 10.14.1, Xcode 10.1, Swift 4.2, Cocoa App]

This is my first Cocoa project, having done a modest amount of iOS development in the past. I've spent two days now trying this and that to no avail. I can't believe how difficult it is to get this to work on MacOS, considering how simple and effective it is in Visual Studio with Windows.

So, I created a straight forward Cocoa App project and the Main.storyboard comes with:
1) A NSApplication (menu),
2) A NSWindowController that sits above the NSWindow,
3) A NSWindow that shows "View Controller" in the middle,
4) A NSViewController, and
5) A NSView (where I've put all my Cocoa objects).
(Standard out of the box)

I have a bunch of NSTextFields organised inside the NSView and I've daisy chained their nextKeyView outlets into the (looped) order I need. I have other controls inside the NSView, but since I don't want them to be tabbed to, I've either set their Refuses First Responder property to ticked or have their Behaviour set to None. I'm also trying to fix this programmatically and have in the default ViewController.swift file (where all my code resides) a loop that iterates through my textfields and sets some of the textfield's properties.

override func viewWillAppear() {
  for i in myTextFields {
    i.value.isEditable = true
    i.value.isEnabled = true
    i.value.alignment = NSTextAlignment.center
    //      i.value.refusesFirstResponder = true
    if i.key == FunctionsEnum.LSP {
      i.value.becomeFirstResponder()
    }
  }
}

(Where myTextFields is a dictionary mapping FunctionsEnums to the right NSTextField.)

I've also tried to recalculate the order using:

let appWindow = self.view.window
appWindow?.recalculateKeyViewLoop()

But sadly this has no effect. I don't understand why the NSView (or NSViewController) doesn't have a recalculateKeyViewLoop() method since it is the container for all the controls I added. Yes, the NSWindow may need to recalculate over a number of views, but NSView could be able to do its own local calculation, if its the only view.

I've read these other questions/answers on this subject:
Setting a custom tab order in a Cocoa application
https://www.stevestreeting.com/2014/02/11/auto-layout-and-tab-ordering/
Tab Order in interface builder?
but none of the offered advice, nor solutions seem to work for me.

Could someone please help me with this?

Do I need to create a sub-class for the NSWindowController or NSWindow? Have I put things in the wrong container? Do I need delegation between the NSViewController/NSView (ViewController.swift) something else? Have I got some other needed code missing? Do I need to invoke some protocols I've left out?

gone
  • 1,079
  • 5
  • 13
  • 31
  • if you want to set new key responder: call makefirstresponder on your window with desired new responder. If you have window in your XIB/Storyboard you can enable recalculateviewloop there (best for tableviews). Also look at property called initialFirstResponder. You can recalculate view loop manually anywhere like nsviewcontroller (it's just about setting nextKeyViews). Recalculate only if you change views dynamically – Marek H Mar 17 '19 at 08:22
  • Possible duplicate of [Cocoa: NextKeyView - Tab Order](https://stackoverflow.com/questions/8794520/cocoa-nextkeyview-tab-order) – Willeke Mar 17 '19 at 10:01

0 Answers0