我目前正在編寫基於文檔的應用程序,並且很好奇我將如何在應用程序中默認加載最近使用的文檔('文件>打開最近'部分)啓動?有關此代碼片段的任何良好信息?新來的OBJ-CMac App:上次使用的「自動加載」文檔
感謝, 扎克
我目前正在編寫基於文檔的應用程序,並且很好奇我將如何在應用程序中默認加載最近使用的文檔('文件>打開最近'部分)啓動?有關此代碼片段的任何良好信息?新來的OBJ-CMac App:上次使用的「自動加載」文檔
感謝, 扎克
在您的應用程序委託,你想實現applicationShouldOpenUntitledFile:
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
NSURL *lastURL=[[[NSDocumentController sharedDocumentController] recentDocumentURLs] objectAtIndex:0];
if (lastURL!=nil)
{
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:lastURL display:YES error:nil];
return NO;
}
return YES;
}
顯然,在Xcode中基於NSDocument默認可可應用程序沒有按」 t來一個AppDelegate。奇怪的。要添加一個:
在爲MyAppDelegate接口,添加的協議NSApplicationDelegate,它應該是這個樣子:
@interface MyAppDelegate:NSObject的<NSApplicationDelegate> {}
打開MainMenu.xib
你現在很好走。
所以上面的代碼幾乎是完美的。我碰到的唯一問題是,如果這是一個新的應用程序,那麼recentDocumentURL的列表可能是空的(或nil),所以要求得到一個nil數組的objectAdIndex:0會導致問題。我建議在方法聲明後面加上下面的代碼。
NSArray *list = [[NSDocumentController sharedDocumentController] recentDocumentURLs];
if ((list == nil) || ([list count] == 0))
return YES;
我想避免的錯誤如下:
2014年1月28日17:02:33.042 jNotebook [8144:303] * - [__ NSArrayM objectAtIndex:]:索引0超出範圍爲空陣列
所以這將在我的主(.m)文件? myDocument.m? – Zakman411 2011-01-23 04:54:58