2012-07-13 47 views
2

有沒有什麼辦法可以將URL傳遞給我的系統上的一個文件夾,該文件夾應該是NSOpenPanel打開的默認窗口? 謝謝!使NSOpenPanel打開一個自定義目錄

更新:

NSOpenPanel *ads_open = [[NSOpenPanel openPanel] retain]; 
[ads_open setDirectoryURL:"file://localhost/System/Library/CoreServices/prndrv"]; 

我使用上面的代碼這是我想在默認情況下打開的目錄。但是,我得到的默認窗口仍然是我訪問的最後一個窗口,而不是我指定的窗口。我如何訪問URL目錄?

+0

怎麼會這樣,即使工作。 'setDirectoryURL:'NSURL不是Char * ... – 2012-07-13 11:19:59

+0

那麼我應該添加什麼路徑作爲NSURL? – Kevin 2012-07-13 11:21:30

+0

[NSURL URLWithString:@ 「文件://你的/路徑/這裏」] – 2012-07-13 11:22:12

回答

0

工作例如:

NSOpenPanel *panel = [NSOpenPanel openPanel]; 
[panel setDirectoryURL:[NSURL URLWithString:@"file://localhost/System/Library/CoreServices/"]]; 

[panel beginSheetModalForWindow:self.window 
       completionHandler:^(NSInteger returnCode) { 
        if (returnCode == NSOKButton) 
        { 
         NSURL *theURL = [[panel URLs] objectAtIndex:0]; 
        } 
       }]; 
+0

這是應該做的伎倆行,但由於某種原因,是不是... \t [ads_open setDirectoryURL:[NSURL URLWithString :@ 「文件://本地主機/系統/圖書館/ CoreServices/HOBmacgate/prndrv」]]; 文件瀏覽器仍在加載已訪問的最後一個URL,而不是指定爲URL的默認URL。 – Kevin 2012-07-13 11:44:15

+0

你確定'/ localhost/System/Library/CoreServices/HOBmacgate/prndrv'確實存在嗎?其他的'NSOpenPanel'不能顯示目錄。 – Anne 2012-07-13 12:02:52

1
NSOpenPanel *ads_open = [NSOpenPanel openPanel]; 
[ads_open setDirectoryURL:[NSURL URLWithString:@"file://localhost/System/Library/CoreServices/prndrv"]]; 
1

提防使用[NSURL fileURLWithPath:someStringPath]否則,它不是一個有效的文件網址:

NSOpenPanel *panel = [NSOpenPanel openPanel]; 

// changes promt to Select 
[panel setPrompt:@"Select"]; 

// Enable the selection of files in the dialog. 
[panel setCanChooseFiles:NO]; 

// Enable the selection of directories in the dialog. 
[panel setCanChooseDirectories:YES]; 

//allows multi select 
[panel setAllowsMultipleSelection:NO]; 
if(exists){ 
    [panel setDirectoryURL:[NSURL fileURLWithPath:lastPath]]; 
} 

[panel beginSheetModalForWindow:self.window 
       completionHandler:^(NSInteger returnCode) { 
        if (returnCode == NSOKButton) 
        { 
         ..... 

       }}]; 
0

對於斯威夫特3:

let panel = NSOpenPanel() 
panel.directoryURL = URL(fileURLWithPath: "smb://servername/path", isDirectory: true)