2012-03-01 24 views
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的我得到的名字,我要的是顯示窗口上的名字也表明,這些文件被選中用戶。

哪個視圖可以使用?達到這個目標的方法是什麼?

感謝名單

回答

1

使用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

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; 
0

runModalForDirectory:file:types:OS X v10.6中棄用。您可以改用runModal。 您可以使用setDirectoryURL:來設置路徑,並且可以使用setAllowedFileTypes:來設置文件類型。

相關問題