Window restoration in document based app (Swift, Storyboards)

Hi all,


I have a document based app where I'm trying to restore an Inspector window when the app restarts.


Window restoration for the documents works well using willEncodeRestorableState and didDecodeRestorableState.


In my Obj-C apps I used this code with success:


+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler
{
    NSWindow *resultWindow = nil;
  
    if ([identifier isEqualToString:@"Inspector"]) {
        /
        resultWindow = [[InspectorPanelController sharedInspectorPanelController] window];
    } else {
        /
        resultWindow = nil;
    }
  
    /
    completionHandler(resultWindow,nil);
}


But now I'm trying to do the same in my new project using Swift and Storyboards... but it just doesn't work. This is what I got this far...


class InspectorWindowController: NSWindowController {
   
    override func windowDidLoad()
    {
        super.windowDidLoad()
       
        window!.restorationClass = self.dynamicType
     }
   
    internal static func restoreWindowWithIdentifier(identifier: String, state: NSCoder, completionHandler: ((NSWindow?,NSError?) -> Void))
    {
        if identifier == "Inspector" {
            let storyboard = NSStoryboard (name: "Main", bundle: nil)
            let controller = storyboard.instantiateControllerWithIdentifier ("InspectorWindowController") as! InspectorWindowController
            let window = controller.window
           
            completionHandler(window, nil)
         }
    }
}


The Inspector window is never restored even though the completion handler of restoreWindowWithIdentifier returns a window?


Anyone got this working in Swift using Storyboards?


Best regards / Jörgen

Replies

a. What have you done to ensure that the window is shown, i.e. made visible?


b. The statement "The Inspector window is never restored" isn't well-defined because restoration is a bunch of little things, not one big thing. So …


c. Do you have evidence that restoreWindowWithIdentifier is being called at all? Being called with a window whose identifer is "Inspector"? Is returning a non-nil window from 'controller.window'?


d. If all that seems to be correct, but the window does not appear, what is its state? Is it offscreen somewhere? Zero size? Under something else?


e. Are you certain the window actually was given the correct restoration class originally?


f. How are you keeping the window controller alive after instantiating it? In the above code, the window controller is going to be deallocated at the end of restoreWindowWithIdentifier, unless you have code somewhere else (e.g. in windowDidLoad) to stash the pointer somewhere.


g. Did you look for bugs unrelated to restoration, such as something else closing the window, etc?


P.S. It looks like a bug in your Swift version of restoreWindowWithIdentifier that you don't invoke the completion handler if the identifier is not "Inspector". Is this bug in your real code, too?