Search code examples
swiftxcodemacosxib

How to open second NSWindow?


I'm trying to open a second NSWindow when an NSTableViewCell is clicked. So far, here's my code:

func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
    IndiFlightWC.loadWindow()
    IndiFlightWC.showWindow(nil)
    IndiFlightWC.setMap(indiFlight: FlightList[row]!)
    return true
}

and the IndiFlightWC is initialized like this:

var IndiFlightWC = IndiFlightWindowController()

I put a breakpoint in the windowDidLoad of my IndiFlightWindowController and it never reaches it. So my question is how to do you properly display a new window?

Thanks!


Solution

  • Your init method is almost certainly incorrect.

    If you look at the documentation for NSWindowController, you'll see there are four different init methods listed there. All of them take parameters.

    Is your IndiFlightWindowController xib included in your project? Why not try let indiFlightWC = IndiFlightWindowController(windowNibName: "IndiFlightWindowController") and see what happens.

    A couple other tips:

    1) variable names should start with lower case. Use indiFlightWC instead of IndiFlightWC as a variable name. Caps should be for classes.

    2) loadWindow isn't needed because showWindow should automatically call loadWindow.