从finder添加项目

时间:2011-05-06 21:19:23

标签: objective-c cocoa finder

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

我该怎么做? 我搜索了开发人员参考,但没有找到它..

1 个答案:

答案 0 :(得分:2)

使用NSOpenPanel

有关处理文件和使用打开面板的指南,请参阅Application File Management指南。

例如:

- (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
            }
        }
    }];

}