How to save and restore position of secondary NSViewController

I have a macOS storyboard with a main Window/View Controller and a couple of "accessory" view controllers that are shown via segues from buttons.

The problem is, even having set the restoration ID of both controllers in the storyboard, that they always start at the same position, even if they have been moved.

Can anyone help me to get them to reopen in the same place they were when they were closed?

Replies

Could you explain how and where you set the restoration ID (in storyboard ?). Do you mean 'Autosave' ?

I found this old thread that explains there is a problem with autoSave when called from a segue (that seems to be your case):

https://stackoverflow.com/questions/25150223/nswindowcontroller-autosave-using-storyboard

I don't know if there is a better way, but the problem here is that when the AutosaveName is set in Interface Builder, and the Window is open via a Segue, the window is open at a predefined location and the frame is saved, overwriting the last saved frame...
If you have a predefined position to the center of the screen, each time the window will be open, it will appear in the center and the position will be saved.
To avoid this, I set the AutosaveName in the Window Controller (Not in IB), after restoring the saved Frame:

Code Block
class MyWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
let thewindow = window as! NSWindow
/// restore position
thewindow.setFrameUsingName("MyWindow")
self.windowFrameAutosaveName = "MyWindow"
}
}

Another way to do this and have it working even after closing the app, is to save position in UserSettings. And reuse it when opening.
Thank you Claude for your reply.

When I designed the storyboard, I hadn't added a window controller for each of the segued view controllers and hadn't realised that it would be necessary just to save the position of them.

I have added them and created a NSWindowController subclass to add in the necessary code.
So, does it work now ? I hope so.

If so, please don't forget to close the thread.