Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create NSWindow from nib name

Tags:

swift

cocoa

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 init(...) 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?


1 Answers

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)
    }

}
like image 104
juniperi Avatar answered Oct 21 '25 16:10

juniperi