0

I am trying with no success to use instantiateController(identifier: creator:) to create a window controller.

Here is the code :

class MyOwnWindowController: NSWindowController {
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(_ aNotification: Notification) {

        let storyboard  = NSStoryboard(name:    NSStoryboard.Name("Main"),
                                       bundle:  nil)

        let sceneIdentifier     = NSStoryboard.SceneIdentifier("main-window-controller")
        let windowController    = storyboard.instantiateController(identifier: sceneIdentifier,
                                                                   creator: { coder in
            MyOwnWindowController.init(coder: coder)
        })

        windowController.showWindow(self)
    }
}

Of course, I have a storyboard with a window controller scene with id "main-window-controller" (and of class MyOwnWindowController).

The run crashes inside the instantiateController method. I can see that my init(coder:) method is correctly called. The following message is printed in the console :

2020-04-18 09:38:33.307506+0200 TestApp[1880:43088] *** Assertion failure in -[NSClassSwapper _createControllerForCreator:coder:], /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1894.40.150/IB.subproj/NSClassSwapper.m:182
2020-04-18 09:38:33.307724+0200 TestApp[1880:43088] Custom instantiated controller must call -[super initWithCoder:]

And it is obvious that [super initWithCoder:] is called...

I have tried in Swift and Objective-C, and I get the same result.

So how to use instantiateController(identifier: creator:) ?

AirXygène
  • 2,409
  • 15
  • 34
  • 2
    Does this answer your question? [Can't use storyboard custom instantiated window controller](https://stackoverflow.com/questions/60343551/cant-use-storyboard-custom-instantiated-window-controller) – Scratte Apr 20 '20 at 19:48
  • 1
    Yes, thanks. I realized my question was a duplicate, so I answered myself just below. I also requested my question to be closed as duplicate. – AirXygène Apr 20 '20 at 21:17

0 Answers0