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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5621513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:56:41  来源:igfitidea点击:

How do I change "Open" to "Select" in the NSOpenPanel?

cocoamacosobjective-c++nsopenpanel

提问by Amitg2k12

In my Application i need to show the select file dialog, I am making use of the NSOpenPanel which allows to select the file, code is as shown below,

在我的应用程序中,我需要显示选择文件对话框,我正在使用允许选择文件的 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.
        }
    }

}

everything works perfect, but i am facing only one issue, while opening the file, it shows the Open and Cancel button, Is there any way to rename the open button to “Select” button, or do i need to use some other Cocoa Resource.

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

采纳答案by Anne

Add this line:

添加这一行:

[openDlg setPrompt:@"Select"];

回答by Justas

Thanks a lot for the question and answers. I have replaced deprecated methods and it seems to work fine. Sorry not yet sure about editing other people answers (new to contributing here).

非常感谢您的提问和回答。我已经替换了不推荐使用的方法,它似乎工作正常。抱歉还不确定编辑其他人的答案(在这里贡献的新手)。

 - (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.
        }
    }
}