在窗口中显示选定的文件路径/名称 - 可可编程

问题描述:

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

- (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的我得到的名字,我要的是显示窗口上的名字也表明,这些文件被选中用户。

哪个视图可以使用?达到这个目标的方法是什么?

感谢名单

使用NSTextViewNSTextField

+0

它会按行显示多个文件名 – 2012-03-01 08:01:42

+0

它将显示您放入它的任何文本。如果每行需要一个文件名,则在每个文件名后加一个换行符('\ n')。 – 2012-03-01 08:02:39

+0

你能帮我一下代码吗,我该如何显示这个NSArray *文件= [openDlg文件名];在NSTextView – 2012-03-01 08:04:54

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; 

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