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

}

すべてが完璧に機能しますが、ファイルを開いているときに1つの問題だけが発生し、[開く]と[キャンセル]ボタンが表示されます。開くボタンの名前を[選択]ボタンに変更する方法はありますか、それとも他のCocoaリソースを使用する必要がありますか。

4

2 に答える 2

13

次の行を追加します。

[openDlg setPrompt:@"Select"];
于 2011-04-11T13:02:46.773 に答える
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.
        }
    }
}
于 2014-03-10T20:17:32.043 に答える