问题
I'm having some trouble implementing NSWindowRestoration
(in 10.7 Lion). I'm not getting the protocol notifications.
Is there an example app with this implemented somewhere? I cannot find one on the Apple Developer site. Thanks!
Edit: The question marked as answer is helpful, but the problem in my case was that I was using a menubar-only application. I guess window restoration doesn't work with dockless apps yet. Snap!
回答1:
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);
}
}
回答2:
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
.
来源:https://stackoverflow.com/questions/9187624/any-nswindowrestoration-examples