Alex Alex - 1 year ago 221
Swift Question

Create NSWindow from nib name

I'm trying to create an NSWindow from an own nib file but it seems to be harder than I expected...

The NSWindowController offers an

init(windowNibName: String?)
method which could be overridden by a sub-class. It doesn't seem to be possible to invoke the base class'
init(windowNibName: String?)
without overriding it (the compiler shows an error message that there is no initializer which takes that String argument).

But when overriding this
method you have to follow the initialization chain which forces you to call a designated initializer. This designated initializer is
init(window: NSWindow!)

And this is where I'm stuck at the moment... it seems like I have to create an NSWindow object (somehow) in order to pass it to the designated initializer.
But I don't know how to create an NSWindow from a nib file.

Does anybody have any clue?

Answer Source

It's bug, I think.

Create your own init method and load xib file yourself, like this:

class MyWindowController: NSWindowController {

    init() {
        super.init(window: nil)

        /* Load window from xib file */
        NSBundle.mainBundle().loadNibNamed("MyWindow", owner: self, topLevelObjects: nil)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download