Any NSWindowRestoration examples?

ぃ、小莉子 提交于 2019-12-04 11:45:38

The class method + (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler as described by "El Developer" is only half of the solution.

The class that implements the method (and conforms to the NSWindowRegistration protocol) also has to be registered as the window's "Restoration Class". When the window is initially created, register it using the - (void)setRestorationClass:(Class <NSWindowRestoration>)restorationClass method.

e.g. for a window controller, for initialization:

_myWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"];
_myWindowController.window.restorationClass = self.class;
_myWindowController.window.identifier = @"MyWindow";

for restoration:

+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler {

    if ([identifier isEqualToString:@"MyWindow"]) {
        MyAppDelegate *appDelegate = (MyAppDelegate *)NSApplication.sharedApplication.delegate;
        NSWindow *myWindow = appDelegate.myWindowController.window;

        completionHandler(myWindow, nil);
    }
}

There is one little code snipet:

+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler
{
   // Get the window from the window controller,
   // which is stored as an outlet by the delegate.
   // Both the app delegate and window controller are
   // created when the main nib file is loaded.
   MyAppDelegate* appDelegate = (MyAppDelegate*)[[NSApplication sharedApplication] delegate];
   NSWindow* mainWindow = [appDelegate.windowController window];

   // Pass the window to the provided completion handler.
   completionHandler(mainWindow, nil);
}

Found here.

Hopefully this will help you.

Edit:

Be sure you are implementing the protocol in your application class, remember you have to add it in your m file.

@interface MyClass : FatherClass <NSWindowRestoration>

**I'm not 100% of the name of the protocol so that last line could be wrong, sorry I'm in a rush right now, it's either that or NSWindowRestorationDelegate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!