2009-10-28 35 views
7

Tôi đang học Objective-C và cố gắng phát triển một ứng dụng dây kéo đơn giản, nhưng tôi dừng lại khi bây giờ, khi tôi cần chèn một nút vào hộp thoại của tôi và nút này sẽ mở ra một hộp thoại Open File sẽ chọn một tập tin để nén, nhưng tôi không bao giờ sử dụng một Open File Dialog, sau đó làm thế nào tôi có thể mở nó và lưu trữ người dùng được lựa chọn tập tin trong một char*? Cảm ơn.Open File Dialog Box

Hãy nhớ rằng tôi đang sử dụng GNUstep (Linux).

Trả lời

16

Trong trường hợp ai đó cần câu trả lời này, ở đây nó là:

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

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

    // Multiple files not allowed 
    [openDlg setAllowsMultipleSelection:NO]; 

    // Can't select a directory 
    [openDlg setCanChooseDirectories:NO]; 

    // 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(i = 0; i < [files count]; i++) 
    { 
    NSString* fileName = [files objectAtIndex:i]; 
    } 
18

Cảm ơn @Vlad các Impala Tôi cập nhật câu trả lời của bạn cho những người sử dụng OS X v10.6 +

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

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

// Multiple files not allowed 
[openDlg setAllowsMultipleSelection:NO]; 

// Can't select a directory 
[openDlg setCanChooseDirectories:NO]; 

// Display the dialog. If the OK button was pressed, 
// process the files. 
if ([openDlg runModal] == NSOKButton) 
{ 
    // Get an array containing the full filenames of all 
    // files and directories selected. 
    NSArray* urls = [openDlg URLs]; 

    // Loop through all the files and process them. 
    for(int i = 0; i < [urls count]; i++) 
    { 
     NSString* url = [urls objectAtIndex:i]; 
     NSLog(@"Url: %@", url); 
    } 
} 
2

Đối với những người sử dụng OS X v10.10 +, thay thế ở vùng Viễn Jangtrakool câu trả lời:

if ([openDlg runModal] == NSOKButton) 

bởi

if ([openDlg runModal] == NSModalResponseOK) 
Các vấn đề liên quan