Search code examples
xcodemacoscocoastoryboardosx-yosemite

NSWindowController Autosave using Storyboard


I have a Swift application that is launching a simple NSWindow, like so:

func applicationDidFinishLaunching(notification: NSNotification!) {
    let storyboard = NSStoryboard(name: "MainStoryboard", bundle: NSBundle.mainBundle())
    windowController = storyboard.instantiateInitialController() as? NSWindowController
    windowController?.showWindow(self)
}

This works great, my app launches and the window appears. However: The size is always the same.

In the storyboard, I have specified an Autosave name:

MainStoryboard

Note also the Restorable checkbox is checked.

But regardless, the window appears the same size, every time. This has always "just worked" in the past, so I can't tell if this is a bug, or a piece I'm missing. Does autosaving automatically work with storyboards when instantiating and showing windows manually?


Solution

  • This seems to be an Xcode bug. I was able to workaround it by manually setting the NSWindowController windowFrameAutosaveName property:

    windowController?.windowFrameAutosaveName = "Main App Window"

    However... This only worked for me if the property was set to a different value than what is displayed in Interface Builder. If it's programmatically set to the same value that's being used in IB, it doesn't work.

    So in IB the autosave name is left to MainAppWindow, and programmatically it's set to Main App Window.