繁体   English   中英

NSOpenPanel的setDirectoryURL不起作用

[英]NSOpenPanel's setDirectoryURL doesn't work

我正在尝试使用NSOpenPanel的新方法并设置其初始目录。 问题是它只在第一次工作,然后它只是“记住”最后选择的文件夹,我不想要。 我必须使用折旧的runModalForDirectory:file:使它工作。 它不太理想,因为它被弃用了10.6,但幸运的是它仍适用于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"]];

有几件事需要研究:

  1. ~/Pictures不是有效的网址。 file:///Users/user/Pictures是。 -[NSURL URLWithString:]需要有效的URL。 您可能想要使用-[NSURL fileURLWithPath:]代替。 它将/Users/user/Pictures转换为file:///Users/user/Pictures
  2. Tildes不会自动展开,因此您需要使用[@"~/Pictures stringByExpandingTildeInPath]来获取实际的文件路径。

放在一起,将最后一行更改为:

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

我认为这应该有效。

Lion中的面板需要一个URL:file:// localhost / Library / Desktop Pictures,但您的URL以实际路径开头。 请改用[NSURL fileURLWithPath:@"/Library/Desktop Pictures"]

快乐的编码!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM