NSWindowController Autosave using Storyboard

前端 未结 3 1391
一整个雨季
一整个雨季 2021-02-13 05:08

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

func applicationDidFinishLaunching(notification: NSNotification!) {
    let         


        
相关标签:
3条回答
  • 2021-02-13 05:37

    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.

    0 讨论(0)
  • 2021-02-13 05:39

    In order to satisfy the two conditions identified in the accepted answer and comments, subclassing the window controller seems to work. You can then avoid setting this property uniquely in code for every window controller and simply specify the storyboard's window autosave property (and set the window controller's subclass).

    The WindowController's windowFrameAutosaveName must be

    1. not blank
    2. different to Window's frameAutosaveName
    class AutoFrameSavingWindowController: NSWindowController
    {
        override func awakeFromNib() {
            if let autosaveName = window?.frameAutosaveName {
                windowFrameAutosaveName = autosaveName + " temp"
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-13 05:44

    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:

    class MyWindowController: NSWindowController {
        override func windowDidLoad() {
            super.windowDidLoad()
    
            let thewindow = window as! NSWindow
    
            /// restore position
            thewindow.setFrameUsingName("MyWindow")
            self.windowFrameAutosaveName = "MyWindow"
        }
    }
    
    0 讨论(0)
提交回复
热议问题