任何NSWindowRestoration的例子?

Yep*_*Yep 3 macos cocoa objective-c nswindow osx-lion

我在实施方面遇到了一些麻烦NSWindowRestoration(在10.7 Lion中).我没有收到协议通知.

是否有一个示例应用程序在某处实现?我在Apple Developer网站上找不到一个.谢谢!


编辑:标记为答案的问题很有帮助,但我的案例中的问题是我使用的是仅限菜单栏的应用程序.我想窗口恢复不适用于无人应用程序.瞬间!

Rya*_*yan 6

+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler"El Developer"描述的类方法只是解决方案的一半.

实现该方法的类(并符合NSWindowRegistration协议)也必须注册为窗口的"恢复类".最初创建窗口时,使用该- (void)setRestorationClass:(Class <NSWindowRestoration>)restorationClass方法注册它.

例如,对于窗口控制器,用于初始化:

_myWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"];
_myWindowController.window.restorationClass = self.class;
_myWindowController.window.identifier = @"MyWindow";
Run Code Online (Sandbox Code Playgroud)

恢复:

+ (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);
    }
}
Run Code Online (Sandbox Code Playgroud)