2011-01-24 48 views
0

我在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?我尝试将它从文件所有者拖到窗口,甚至没有显示在下拉菜单中。或者我完全迷失了?谢谢大家的帮助,我不知道我会没有你们

编辑:

新代码看起来像 -

- (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; 
} 

回答

0

[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:lastURL display:YES error:nil];为在URL线以上。

+0

Josh, 感谢您的意见。我在上面添加了修改后的代码 - 但是,代码仍然不起作用。当我构建+运行应用程序时,以前的文档无法打开(尽管它在“打开最近”中显示@ index 0。 – Zakman411 2011-01-24 02:10:18