Как изменить «Открыть» на «Выбрать» в NSOpenPanel? - PullRequest
22 голосов
/ 11 апреля 2011

В моем приложении мне нужно показать диалог выбора файла, я использую 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.
        }
    }

}

все работает отлично, но яЯ сталкиваюсь только с одной проблемой, при открытии файла она показывает кнопку «Открыть и отменить», есть ли способ переименовать кнопку «Открыть» в кнопку «Выбрать», или мне нужно использовать какой-либо другой ресурс Какао.

Ответы [ 2 ]

13 голосов
/ 11 апреля 2011

Добавить эту строку:

[openDlg setPrompt:@"Select"];
3 голосов
/ 11 марта 2014

Большое спасибо за вопрос и ответы.Я заменил устаревшие методы, и это, кажется, работает нормально.Извините, пока не уверен насчет редактирования ответов других людей (новшество здесь).

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