Cocoa非文档型应用程序中的"另存为"

8

我正在开发一个项目,该项目使用“sox”通过shell脚本创建文件。该应用程序不基于文档,它只是调用一个创建文件的脚本,并不在内部保存任何数据。但是,在运行脚本之前,我需要提示用户选择文件保存路径和文件名。有什么最好的方法可以弹出“另存为”对话框,以便从用户那里获取文件路径/文件名并传递给shell脚本?

1个回答

21

这很简单。你使用了NSSavePanel标签来发起问题,这正是你想要使用的。

- (IBAction)showSavePanel:(id)sender
{
    NSSavePanel * savePanel = [NSSavePanel savePanel];
    // Restrict the file type to whatever you like
    [savePanel setAllowedFileTypes:@[@"txt"]];
    // Set the starting directory
    [savePanel setDirectoryURL:someURL];
    // Perform other setup
    // Use a completion handler -- this is a block which takes one argument
    // which corresponds to the button that was clicked
    [savePanel beginSheetModalForWindow:someWindow completionHandler:^(NSInteger result){
        if (result == NSFileHandlingPanelOKButton) {
            // Close panel before handling errors
            [savePanel orderOut:self];
            // Do what you need to do with the selected path
        }
    }];
}

请参见“文件系统编程指南”中的“保存面板”


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接