为什么不调用applicationShouldOpenUntitledFile?

Ste*_*her 9 macos cocoa appkit

applicationShouldOpenUntitledFile向我的应用程序委托添加了一个方法,返回NOApple的文档指定.但是,我仍然在启动时获得一份新文档.怎么了?

@implementation AppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLog( @"This is being called" );
}

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    NSLog( @"This never is" );
    return NO;  
}

@end
Run Code Online (Sandbox Code Playgroud)

Ste*_*her 10

你正在运行Lion.在添加applicationShouldOpenUntitledFile处理程序之前运行时,会创建一个新文档.现在,随着10.7的"狭路相逢时,恢复窗口,并重新打开应用程序",你的应用程序恢复命名的窗口,而不是创造一个新的,你想.

关闭该窗口并重新运行您的应用程序,applicationShouldOpenUntitledFile将被调用并将禁止创建新的无标题文件.


jee*_*yul 6

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