8

有什么方法可以将 URL 传递到系统上的文件夹,该文件夹应该是 NSOpenPanel 打开的默认窗口?谢谢!

更新:

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

我正在使用上面的代码,这是我希望默认打开的目录。但是,我得到的默认窗口仍然是我访问的最后一个窗口,而不是我指定的窗口。如何访问 URL 目录?

4

4 回答 4

2
NSOpenPanel *ads_open = [NSOpenPanel openPanel];
[ads_open setDirectoryURL:[NSURL URLWithString:@"file://localhost/System/Library/CoreServices/prndrv"]];
于 2012-07-13T11:24:02.933 回答
2

小心使用,[NSURL fileURLWithPath:someStringPath]否则它不是有效的文件 url:

   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)
                  {
                      .....

              }}];
于 2013-06-16T14:00:38.240 回答
2

对于斯威夫特 3:

let panel = NSOpenPanel()
panel.directoryURL = URL(fileURLWithPath: "smb://servername/path", isDirectory: true)
于 2016-11-02T19:17:22.920 回答
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];
                  }
              }];
于 2012-07-13T11:23:24.417 回答