2011-01-24 47 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