首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有NSWindowRestoration的例子吗?

有NSWindowRestoration的例子吗?
EN

Stack Overflow用户
提问于 2012-02-08 03:50:49
回答 2查看 2.2K关注 0票数 5

我在实现NSWindowRestoration (在10.7Lion中)时遇到了一些困难。我没有收到协议通知。

有没有在某个地方实现此功能的示例应用?我在苹果开发者的网站上找不到。谢谢!

编辑:标记为答案的问题是有帮助的,但在我的例子中,问题是我使用的是一个仅限菜单的应用程序。我想窗口恢复还不适用于无坞应用程序。抓紧!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-01 02:34:07

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

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

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

代码语言:javascript
复制
_myWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"];
_myWindowController.window.restorationClass = self.class;
_myWindowController.window.identifier = @"MyWindow";

恢复:

代码语言:javascript
复制
+ (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);
    }
}
票数 6
EN

Stack Overflow用户

发布于 2012-02-08 04:05:33

有一个小代码片段:

代码语言:javascript
复制
+ (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);
}

找到了这里

希望这能帮到你。

编辑:

一定要在应用程序类中实现协议,记住必须将它添加到m文件中。

@interface MyClass : FatherClass <NSWindowRestoration>

**我不是协议的100%的名字,所以最后一行可能是错的,对不起,我现在很着急,要么是那个,要么是NSWindowRestorationDelegate

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9187624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档