【问题标题】:Open File Dialog Box打开文件对话框
【发布时间】:2010-12-11 01:22:25
【问题描述】:
我正在学习 Objective-C 并尝试开发一个简单的 zipper 应用程序,但是我现在停止了,当我需要在我的对话框中插入一个按钮并且这个按钮打开一个打开文件对话框,它将选择一个要压缩的文件,但我从未使用过打开文件对话框,那么我如何打开它并将用户选择的文件存储在char* 中?谢谢。
记住我使用的是 GNUstep(Linux)。
【问题讨论】:
标签:
objective-c
user-interface
dialog
openfiledialog
gnustep
【解决方案1】:
对于使用 OS X v10.10+ 的人,请替换为 Far Jangtrakool 答案:
if ( [openDlg runModal] == NSOKButton )
由
if ( [openDlg runModal] == NSModalResponseOK )
【解决方案2】:
感谢@Vlad the Impala 我正在为使用 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);
}
}
【解决方案3】:
如果其他人需要这个答案,这里是:
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];
}