我正在尝试使用 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"]];
有几件事需要调查:
-
~/Pictures
不是有效的 URL。file:///Users/user/Pictures
is. -[NSURL URLWithString:]
需要有效的 URL。你可能想使用-[NSURL fileURLWithPath:]
反而。它会转动/Users/user/Pictures
into file:///Users/user/Pictures
.
- 波形符不会自动扩展,因此您需要使用
[@"~/Pictures stringByExpandingTildeInPath]
获取实际的文件路径。
放在一起,将最后一行更改为:
[panel setDirectoryURL:[NSURL fileURLWithPath:[@"~/Pictures" stringByExpandingTildeInPath]]];
我认为这应该有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)