使NSOpenPanel打开自定义目录

时间:2012-07-13 10:19:06

标签: objective-c cocoa nsopenpanel

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

更新:

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

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

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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)
                  {
                      .....

              }}];

答案 2 :(得分: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];
                  }
              }];

答案 3 :(得分:0)

对于Swift 3:

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