实施“自动加载”功能时出现问题

时间:2011-01-24 01:11:14

标签: objective-c cocoa xcode macos interface-builder

我在StackOverflow上收到了一些有用的代码,用于在基于文档的应用程序中加载上次使用的文档。但是,我是可可的新手并且在实施它时遇到了麻烦。这是代码:

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    NSURL *lastURL=[[[NSDocumentController sharedDocumentController] recentDocumentURLs] objectAtIndex:0];
    if (lastURL!=nil)
    {
        [docController openDocumentWithContentsOfURL:lastURL display:YES error:nil];    
        return NO;
    }

    return YES;
}

我遇到'docController'问题。我在哪里声明这个?我会假设我把它放在我的头文件中:

IBOutlet NSDocument *docController;

但是我将它连接到IB?我尝试将它从文件所有者拖到窗口,它甚至没有显示在drowdown上。或者我完全迷失了?谢谢大家的帮助,我不确定没有你们我会做什么

编辑:

新代码看起来像 -

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    NSURL *lastURL=[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:lastURL display:YES error:nil];
    if (lastURL!=nil)
    {
     [docController openDocumentWithContentsOfURL:lastURL display:YES error:nil];    
        return NO;
    }

    return YES;
}

1 个答案:

答案 0 :(得分:0)

应该是[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:lastURL display:YES error:nil];,如上面的网址行所示。