Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to make a NSWindowController Singleton in Swift?

I have a sample project as:

https://github.com/ericgorr/nspanel_show.git

My project is a storyboard, document based application. I would like to use a custom segue to toggle the visible state of the inspector window. What I have should work, but I cannot quite determine how to make the inspector window a singleton.

I believe I should start with:

class InspectorWindowController: NSWindowController
{
    static let sharedInstance = InspectorWindowController()

//    override func init()
//    {
//        
//    }

    override func windowDidLoad()
    {
        super.windowDidLoad()

        NSLog( ":::: %@", InspectorWindowController.sharedInstance );
    }
}

But exactly what the initialization should look like in my situation is escaping me, especially since the window is inside of a storyboard.

like image 339
ericg Avatar asked Dec 12 '25 05:12

ericg


1 Answers

You can select the window controller from the window controller scene and in the attributes inspector select Single from the pop up under Presentation. This will ensure the show segue only uses a single instance of the window controller. See this answer for more information.

like image 153
Andrew Avatar answered Dec 14 '25 03:12

Andrew