我正在尝试设置文件选择对话框允许的文件类型,但该对话框没有过滤允许的类型,它允许我上传任何文件类型,
但我只希望它上传 HTML 文件,我还希望不允许的文件在对话框中淡出。
OSX 10.9 支持这种过滤文件类型的方法吗?我收到弃用警告。
- (IBAction)openfile:(id)sender {
int i;
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setAllowedFileTypes:@[@"html", @"htm"]];
if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
NSArray* files = [openDlg filenames];
for( i = 0; i < [files count]; i++ )
{
NSString* fileName = [files objectAtIndex:i];
NSLog(@"%@", fileName);
}
}
}
最佳答案
setAllowedFileTypes
未已弃用,而是正确的方法
runModalForDirectory
已已弃用,应替换为completionHandler
filenames
也已弃用。使用 NSURL 代替路径(总是:))
现代化:
- (IBAction)openfile:(id)sender {
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setAllowedFileTypes:@[@"html", @"htm"]];
[openDlg beginWithCompletionHandler:^(NSInteger result) {
if(result==NSFileHandlingPanelOKButton) {
for (NSURL *url in openDlg.URLs) {
NSLog(@"%@", url);
}
}
}];
}
关于macos - Cocoa 文件选择对话框允许的文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20790249/