【问题标题】:How do I change "Open" to "Select" in the NSOpenPanel?如何在 NSOpenPanel 中将“打开”更改为“选择”?
【发布时间】:2011-04-11 12:48:28
【问题描述】:

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

【问题讨论】:

  • 顺便说一下,filenames 属性在 OS X 10.6 中被声明为弃用
  • runModalForDirectory:file:types: 在 OS X v10.6 中已弃用。您可以改用 runModal 。可以使用 setDirectoryURL: 设置路径,也可以使用 setAllowedFileTypes: 设置文件类型。

标签: cocoa macos objective-c++ nsopenpanel


【解决方案1】:

添加这一行:

[openDlg setPrompt:@"Select"];

【讨论】:

  • 有没有办法将您选择的文件名显示在标签上?或者一种实际选择文件并将其存储在应用程序中以便以后访问它的方法?谢谢!!
  • 我想是的,在框架中你会得到回调,委托选定的文件名,你可以用它在标签上显示它
  • 嘿,我想在点击选择时关闭 openPanel 你能告诉我怎么可能@Anne
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2021-07-25
    • 2021-06-16
    相关资源
    最近更新 更多