2011-10-19 38 views
2

我正在嘗試爲NSOpenPanel使用新方法並設置其初始目錄。問題是它只在第一次使用,之後它只是「記住」最後選擇的文件夾,我不想要。我必須使用depreciated runModalForDirectory:file:使其工作。這並不理想,因爲它在10.6被棄用,但是幸好它仍然適用於Lion。NSOpenPanel的setDirectoryURL無法在Lion上工作

我的代碼是:

NSOpenPanel *panel = [NSOpenPanel openPanel]; 
[panel setAllowedFileTypes:[NSArray arrayWithObjects: @"jpg",@"JPG",@"png", nil]]; 
panel.canChooseDirectories = YES; 
panel.allowsMultipleSelection = YES; 
handler = ^(NSInteger result) {stuff}; 
[panel setDirectoryURL:[NSURL URLWithString:@"/Library/Desktop Pictures"]]; 

我認爲這是獅子的錯誤....

+0

不要考慮你*想要什麼,而是想要你的用戶想要什麼。話雖如此,它看起來像一個bug。 – JeremyP

回答

7

有幾件事情要考慮:

  1. ~/Pictures是不是一個有效URL。 是。 -[NSURL URLWithString:]需要有效的URL。您可能需要使用-[NSURL fileURLWithPath:]。它會將/Users/user/Pictures變成。
  2. Tildes不會自動展開,因此您想使用[@"~/Pictures stringByExpandingTildeInPath]來獲取實際的文件路徑。

放在一起,改變最後一行:

[panel setDirectoryURL:[NSURL fileURLWithPath:[@"~/Pictures" stringByExpandingTildeInPath]]]; 

我認爲應該工作。

+0

感謝您的回答,但正如我所說的那樣,它第一次工作。我將問題中的路徑更新爲「/ Library/Desktop Pictures」,這也不起作用(我從那開始)。謝謝 – Tibidabo

+0

[NSURL fileURLWithString:[@「〜/ path/to/dir」stringByExpandingTildeInPath]]爲我工作。謝謝。 – alesplin

+1

我找不到'fileURLWithString:'但我可以找到'URLWithString:'。這是一個錯字嗎? – Todd

3

Lion中的面板需要URL:file:// localhost/Library/Desktop Pictures,但URL以實際路徑開頭。 改爲使用[NSURL fileURLWithPath:@"/Library/Desktop Pictures"]

快樂編碼!

相關問題