NSOpenPanel的setDirectoryURL不起作用 [英] NSOpenPanel's setDirectoryURL doesn't work

查看:186
本文介绍了NSOpenPanel的setDirectoryURL不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对NSOpenPanel使用新方法并设置其初始目录.问题在于它仅在第一次使用后才记住"最后一个选定的文件夹,而这是我不想要的.我必须使用已贬值的runModalForDirectory:file:使其工作.它不理想,因为它已在10.6弃用,但值得庆幸的是,它仍然适用于Lion.

I'm trying to use the new methods for NSOpenPanel and set its initial directory. The problem is that it only works at the first time and after that it just "remembers" the last selected folder, which I don't want. I have to use the depreciated runModalForDirectory:file: to make it work. It's less than ideal because it was deprecated at 10.6, but thankfully it still works on 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是无效的URL. file:///Users/user/Pictures是. -[NSURL URLWithString:]需要一个有效的URL.您可能想使用-[NSURL fileURLWithPath:]代替.它将把/Users/user/Pictures变成file:///Users/user/Pictures.
  2. 波浪不会自动扩展,因此您想使用[@"~/Pictures stringByExpandingTildeInPath]来获取实际的文件路径.
  1. ~/Pictures is not a valid URL. file:///Users/user/Pictures is. -[NSURL URLWithString:] requires a valid URL. You probably want to use -[NSURL fileURLWithPath:] instead. It will turn /Users/user/Pictures into file:///Users/user/Pictures.
  2. Tildes are not automatically expanded, so you want to use [@"~/Pictures stringByExpandingTildeInPath] to get an actual file path.

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

Put together, change the last line to:

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

我认为应该可以.

这篇关于NSOpenPanel的setDirectoryURL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆