Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't applicationShouldOpenUntitledFile being called?

I added a applicationShouldOpenUntitledFile method to 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
like image 547
Steven Fisher Avatar asked Sep 27 '11 04:09

Steven Fisher


2 Answers

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.

like image 133
Steven Fisher Avatar answered Dec 27 '22 20:12

Steven Fisher


-(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];
    }
}
like image 40
jeeeyul Avatar answered Dec 27 '22 20:12

jeeeyul