NSOpenPanel的setDirectoryURL在Lion上不工作 [英] NSOpenPanel's setDirectoryURL doesn't work on Lion

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

问题描述

我试图使用NSOpenPanel的新方法并设置其初始目录。问题是,它只工作在第一次,之后,它只是记住最后一个选定的文件夹,我不想要的。我必须使用折旧的runModalForDirectory:file:使它工作。这不太理想,因为它在10.6被弃用,但幸运的是它仍然在狮子。

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"]];

我认为这是Lion的错误....

I think it's a bug in Lion....

推荐答案

有几件事要研究:


  1. 〜/ Pictures 不是有效的网址。 file:/// Users / user / Pictures is。 - [NSURL URLWithString:] 需要有效的网址。你可能想改用 - [NSURL fileURLWithPath:] 。它会将 / Users / user / Pictures 变成 file:/// Users / user / Pictures 。 >
  2. Tildes不会自动展开,因此您要使用 [@〜/图片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.

将最后一行改为:

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

我认为这应该工作。

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

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