Mike Nathas
Mike Nathas

Reputation: 1393

Open NSWindow from xib while using storyboards and swift

I am using storyboards and swift. Now I am trying to open a new NSWindow which is packed inside a xib file. (I've created a new xib file which contains a NSWindow)

I've added the following code to my project, but it does not open any window.

let controller = NSWindowController(windowNibName: "xibFile")
controller.showWindow(self)

I am sure I'm doing something wrong as

print(controller.window?.title)

also won't give me the correct window title from the xib file.

Hope anyone can give a hint what I'm doing wrong. Thanks!

Upvotes: 2

Views: 1923

Answers (1)

Code Different
Code Different

Reputation: 93161

In your second nib:

class SecondNibController: NSWindowController {
    override var windowNibName: String? {
        return "SecondNib" // no extension .xib here
    }
}

Remember to connect the window outlet of SecondNibConntroller to your window.

In your main storyboard:

class ViewController: NSViewController {
    let secondNibController = SecondNibController()

    @IBAction func openSecondWindow(sender : AnyObject) {
        secondNibController.showWindow(nil)
    }
}

Upvotes: 4

Related Questions