1

我有一张桌子,下面有经典的 + - 按钮。(在 Mac 上)我想按 + 按钮,然后打开一个小查找器来选择一个文件,然后将其添加到桌面上。

我怎样才能做到这一点?我搜索了开发人员参考,但没有找到它..

4

1 回答 1

2

使用NSOpenPanel.

有关处理文件和使用打开面板的指南,请参阅应用程序文件管理指南。

例如:

- (IBAction)addFile:(id)sender
{
    NSInteger result;
    NSArray *fileTypes = [NSArray arrayWithObject:@"html"];
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];

    [oPanel setAllowsMultipleSelection:YES];
    [oPanel setDirectory:NSHomeDirectory()];
    [oPanel setCanChooseDirectories:NO];
    result = [oPanel runModal];

    if (result == NSFileHandlingPanelOKButton) {
        for (NSURL *fileURL in [oPanel URLs]) {
            // do something with fileURL
        }
    }
}

使用工作表的另一个示例:

- (IBAction)addFile:(id)sender
{
    NSArray *fileTypes = [NSArray arrayWithObject:@"html"];
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];

    [oPanel setAllowsMultipleSelection:YES];
    [oPanel setDirectory:NSHomeDirectory()];
    [oPanel setCanChooseDirectories:NO];
    [oPanel beginSheetModalForWindow:[self window]
        completionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {
            for (NSURL *fileURL in [oPanel URLs]) {
                // do something with fileURL
            }
        }
    }];

}
于 2011-05-06T21:37:11.373 回答