macos - Cocoa 文件选择对话框允许的文件类型

标签 macos cocoa osx-mavericks

我正在尝试设置文件选择对话框允许的文件类型,但该对话框没有过滤允许的类型,它允许我上传任何文件类型,

但我只希望它上传 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/

相关文章:

_NSGetExecutablePath 的 Linux 替代品?

macos - 在 Mac OS Catalina 上安装 Pentaho Data Integration(PDI、Kettle)时出错

python - 在mac上安装smartsheet-python-sdk时出错

cocoa - 处理核心数据模型变更

iphone - 隐藏 UIPageControl

cocoa - 无法对图像应用滤镜

java - Eclipse 未在 OS X Mavericks 中启动? (没有一次退回或错误消息!)

linux - man 找不到与符号链接(symbolic link)指向的源文件关联的手册页

ios - 在 MKMapView 上禁用指南针

cocoa 加密明文?