Mr. Andrew Hansen Mr. Andrew Hansen - 1 year ago 92
Swift Question

Simple displaying of second window: OS X & Swift

I'm trying to find how to bring up a second view/window after pushing a button on my primary window. I have read about segues and I can get the first window to display the second but the second is not connected to a view controller so I can't add any code to any controls on the second view. Try as I might I cannot create a SecondViewController.swift file and connect it to a window controller or a view controller. The tutorials I have found all deal with iOS and I want OS X which means there are just enough differences to keep me from figuring this out.

Can anyone show me how to do this?

Ta,
A.

Answer Source

First make new file like: enter image description here

After that, put these codes in your classes and that should do it.

class SecondWindowController: NSWindowController {

    convenience init() {
        self.init(windowNibName: "SecondWindowController")
    }
}

class ViewController: NSViewController {

    private var secondWindowController: SecondWindowController?

    @IBAction func showSecondWindow(sender: AnyObject) {
        if secondWindowController == nil {
            secondWindowController = SecondWindowController()
        }
        secondWindowController?.showWindow(self)
    }
}