1

我是可可编程的新手,使用下面的代码我想在窗口中显示选定的文件名。我怎样才能做到这一点?

- (IBAction)selectFile:(id)sender {

    int i; // Loop counter.

    // Create the File Open Dialog class.
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    NSArray *fileTypes = [NSArray arrayWithObjects:@"wmv", @"3gp", @"mp4", @"avi", @"mp3", @"mma", @"wav", nil];

    // Enable the selection of files in the dialog.
    [openDlg setCanChooseFiles:YES];

    //Enable multiple selection of files
    [openDlg setAllowsMultipleSelection: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 types:fileTypes] == 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( i = 0; i < [files count]; i++ )
        {
            NSString* fileName = [files objectAtIndex:i];

            NSLog(@"filename::: %@", fileName);

            // Do something with the filename.
        }
    }
}

在 NSLog 中,我正在获取名称,我想要在窗口上显示名称,以向用户显示这些文件已被选中。

可以使用哪个视图?实现这一目标的方法是什么?

谢谢

4

3 回答 3

1

使用NSTextViewNSTextField

于 2012-03-01T07:59:10.360 回答
0
NSArray* files = [openDlg filenames];
NSString* fileName;
    // Loop through all the files and process them.
    for( i = 0; i < [files count]; i++ )
    {
        fileName =[fileName stringByAppendingString:[files objectAtIndex:i];

        // Do something with the filename.
    }

        NSLog(@"filename::: %@", fileName);
      textView.text=fileName;
于 2012-03-01T08:13:04.643 回答
0

runModalForDirectory:file:types:在OS X v10.6中已弃用。你可以runModal改用。您可以使用 设置路径setDirectoryURL:,也可以使用 设置文件类型setAllowedFileTypes:

于 2013-05-28T05:37:13.857 回答