[英]Make NSOpenPanel open a custom directory
有什么方法可以将URL传递给我的系统上的文件夹,该文件夹应该是NSOpenPanel打开的默认窗口? 谢谢!
更新:
NSOpenPanel *ads_open = [[NSOpenPanel openPanel] retain];
[ads_open setDirectoryURL:"file://localhost/System/Library/CoreServices/prndrv"];
我正在使用上面的代码,这是我希望默认打开的目录。 但是,我得到的默认窗口仍然是我访问过的最后一个窗口,而不是我指定的窗口。 如何访问URL目录?
NSOpenPanel *ads_open = [NSOpenPanel openPanel];
[ads_open setDirectoryURL:[NSURL URLWithString:@"file://localhost/System/Library/CoreServices/prndrv"]];
注意使用[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)
{
.....
}}];
工作范例:
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];
}
}];
对于Swift 3:
let panel = NSOpenPanel()
panel.directoryURL = URL(fileURLWithPath: "smb://servername/path", isDirectory: true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.