如何在NSOpenPanel中将“打开”更改为“选择”?

时间:2011-04-11 12:48:28

标签: cocoa macos objective-c++ nsopenpanel

在我的应用程序中,我需要显示选择文件对话框, 我正在使用允许选择文件的NSOpenPanel,代码如下所示,

- (IBAction)sendFileButtonAction:(id)sender{

    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    // Enable the selection of files in the dialog.
    [openDlg setCanChooseFiles:YES];

    // Enable the selection of directories in the dialog.
    [openDlg setCanChooseDirectories:YES];

    // Display the dialog.  If the OK button was pressed,
    // process the files.
    if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
    {
        // Get an array containing the full filenames of all
        // files and directories selected.
        NSArray* files = [openDlg filenames];

        // Loop through all the files and process them.
        for( int i = 0; i < [files count]; i++ )
        {
            NSString* fileName = [files objectAtIndex:i];
            [self log:fileName];

            // Do something with the filename.
        }
    }

}

一切都很完美,但我只面临一个问题,在打开文件时,它会显示“打开”和“取消”按钮, 有没有办法将打开按钮重命名为“选择”按钮,或者我是否需要使用其他一些Cocoa资源。

2 个答案:

答案 0 :(得分:13)

添加以下行:

[openDlg setPrompt:@"Select"];

答案 1 :(得分:3)

非常感谢您提出问题和答案。我已经替换了已弃用的方法,它似乎工作正常。抱歉还不确定如何编辑其他人的答案(此处有新的贡献)。

 - (IBAction)sendFileButtonAction:(id)sender{

    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    // Enable the selection of files in the dialog.
    [openDlg setCanChooseFiles:YES];

    // Enable the selection of directories in the dialog.
    [openDlg setCanChooseDirectories:YES];

    // Change "Open" dialog button to "Select"
    [openDlg setPrompt:@"Select"];

    // Display the dialog.  If the OK button was pressed,
    // process the files.
    if ( [openDlg runModal] == NSModalResponseOK )
    {
        // Get an array containing the full filenames of all
        // files and directories selected.
        NSArray* files = [openDlg URLs];

        // Loop through all the files and process them.
        for( int i = 0; i < [files count]; i++ )
        {
            NSString* fileName = [files objectAtIndex:i];
            NSLog(@"file: %@", fileName);
            // Do something with the filename.
        }
    }
}