Search code examples
cocoaappkitnswindowcontroller

Is encodeRestorableState() supported in NSWindowController?


For the life of me, I can't figure it out. I think I tried everything to have the encodeRestorableState(with:) method get called in my NSWindowController subclass but it just doesn't.

After spending a few hours on this, I'm thinking it might not actually be supported at all. What does this piece of documentation mean then?

enter image description here

Note that it says to use the method instead of overriding/implementing it. Maybe that's where I got it wrong.


Update

Digging further, I found out that the window I create in my NSWindowController subclass has its windowController property set to nil. I guess that's the reason the window doesn't tell the controller to encode its state. I'm now figuring out why it's nil.


Solution

  • Finally figured it out. Basically, that's how I was creating the window controller in AppDelegate:

    MainWindowController().showWindow(self)
    

    I didn't retain any references to the window controller instance, and since NSWindow's windowController property is weak, the window controller was being deallocated. And that's what caused many issues, including the one in this question. I hit other problems because of this as well.