我在OS X上的应用程序中有一些操作,我必须从查找器中选择文件。我想显示类似"打开文件"的窗口。我知道这让我打开带有路径的网址:
[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES]];
但是如何使用"选择"按钮显示窗口。此窗口应该让我获取有关所选文件的信息。
我怎样才能正确地做到这一点?
谢谢你的帮助。
之前答案的代码:
NSOpenPanel *op = [NSOpenPanel openPanel];
op.canChooseFiles = YES;
op.canChooseDirectories = YES;
[op runModal];
self.txtFilePath.stringValue = [op.URLs firstObject];
在操作中。可以找到刚刚选择的所有文件的路径的 URL。
基于EderYif的答案,以下内容不会产生编译器警告,并且还会删除返回文件名的"file://"部分。
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setCanChooseFiles:true];
[op setCanChooseDirectories:true];
[op runModal];
NSString* file = [[op.URLs firstObject] absoluteString];
NSString* fixedFile = [file stringByReplacingOccurrencesOfString:@"file://"
withString:@""];
[[self textFilePath] setStringValue:fixedFile];
@Perception和@omz给了我很好的答案。答案是NSOpenPanel
.