Why isn't applicationShouldOpenUntitledFile being called?
I added a applicationShouldOpenUntitledFile
method t开发者_StackOverflow社区o my application delegate, returning NO
as Apple's documentation specifies. However, I'm still getting a new document on startup. What's wrong?
@implementation AppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog( @"This is being called" );
}
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
NSLog( @"This never is" );
return NO;
}
@end
You're running Lion. When you ran before adding the applicationShouldOpenUntitledFile
handler, a new document was created. Now, with 10.7's "Restore windows when quitting and re-opening apps", your application is restoring that untitled window, and not creating a new one as you suppose.
Close that window and re-run your application, and applicationShouldOpenUntitledFile
will be called and will suppress the creation of a new untitled file.
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
// Schedule "Checking whether document exists." into next UI Loop.
// Because document is not restored yet.
// So we don't know what do we have to create new one.
// Opened document can be identified here. (double click document file)
NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(openNewDocumentIfNeeded) object:nil];
[[NSOperationQueue mainQueue] addOperation: op];
}
-(void)openNewDocumentIfNeeded
{
NSUInteger documentCount = [[[NSDocumentController sharedDocumentController] documents]count];
// Open an untitled document what if there is no document. (restored, opened).
if(documentCount == 0){
[[NSDocumentController sharedDocumentController]openUntitledDocumentAndDisplay:YES error: nil];
}
}
I'm using Xcode 8.3.2 and compiling for Os X 10.11 using a storyboard for a document based app. I noted that, if you set the window controller as initial controller, a window is created without any document and without calling applicationShouldOpenUntitledFile.
I solved removing the "is initial controller" checkbox in the storyboard.
If you're not running Lion / 10.7 or later, this can still happen if you have some other window open (even a non-Document window) when applicationShouldOpenUntitledFile
should be called.
I have a Document-based app where the AppDelegate class opens a global logging window, both for debugging purposes and for user status messages. If I have the program display that window on startup while running on OS X 10.6, applicationShouldOpenUntitledFile
never gets called, even with no document windows displayed. If I turn that window off, the call is made.
Since OSX Lion, the app's state restoration may interfere with your custom preferences for this exercise.
Citing an update to Aaron Hillegass and Adam Preble's book Cocoa Programming for MacOSX:
Note that Mac OS X Lion's state-restoration features may make it tricky to observe the new document preference. You can disable state restoration by editing the Run scheme in Xcode. Open the product menu and select Edit Scheme. Select the Run RaiseMan.app scheme, change to the Options pane, and check Disable state restoration.
精彩评论