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

22

我的应用程序需要显示选择文件对话框,我正在使用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
顺便提一下,在OS X 10.6中,filenames属性已被声明为弃用。 - Max Yankov
runModalForDirectory:file:types:在OS X v10.6中已被弃用。您可以改用runModal。您可以使用setDirectoryURL:设置路径,使用setAllowedFileTypes:设置fileTypes。 - Itachi
2个回答

13

添加这行代码:

[openDlg setPrompt:@"Select"];

有没有办法让你选择的文件名显示在标签上?或者有没有一种方法可以实际选择文件并将其存储在应用程序中,以便以后访问它?谢谢! - user762034
我认为是的,在框架中,您将获得所选文件名的回调和委托,您可以使用它来在标签上显示。 - Amitg2k12
嘿,我想在点击选择时关闭 openPanel,你能告诉我如何做到吗?@安妮 - Gajendra Rawat

4
非常感谢您的提问和回答。我已经替换了过时的方法,看起来一切都正常。抱歉,我还不确定如何编辑其他人的答案(在这里做出贡献还很新)。
 - (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.
        }
    }
}

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