如何在 xcode cocoa 项目中创建打开文件对话框?

发布于 2024-11-27 16:10:58 字数 48 浏览 1 评论 0原文

我正在寻找与 Windows 的 GetOpenFileName 函数等效的函数。

I'm looking for an equivalent to GetOpenFileName function for Windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2024-12-04 16:10:58

Open File UI 的标准 Cocoa 类是 NSOpenPanel

示例代码:

//this gives you a copy of an open file dialogue
NSOpenPanel* openPanel = [NSOpenPanel openPanel];

//set the title of the dialogue window
openPanel.title = @"Choose a .PED texture file";

//shoud the user be able to resize the window?
openPanel.showsResizeIndicator = YES;

//should the user see hidden files (for user apps - usually no)
openPanel.showsHiddenFiles = NO;

//can the user select a directory?
openPanel.canChooseDirectories = NO;

//can the user create directories while using the dialogue?
openPanel.canCreateDirectories = YES;

//should the user be able to select multiple files?
openPanel.allowsMultipleSelection = NO;

//an array of file extensions to filter the file list
openPanel.allowedFileTypes = @[@"ped"];

//this launches the dialogue
[openPanel beginSheetModalForWindow:appDelegate.controlsWindow
                completionHandler:^(NSInteger result) {

                    //if the result is NSOKButton
                    //the user selected a file
                    if (result==NSOKButton) {

                        //get the selected file URLs
                        NSURL *selection = openPanel.URLs[0];

                        //finally store the selected file path as a string
                        NSString* path = [[selection path] stringByResolvingSymlinksInPath];

                        //here add yuor own code to open the file

                    }

                }];

Standard Cocoa class for Open File UI is NSOpenPanel

Example code:

//this gives you a copy of an open file dialogue
NSOpenPanel* openPanel = [NSOpenPanel openPanel];

//set the title of the dialogue window
openPanel.title = @"Choose a .PED texture file";

//shoud the user be able to resize the window?
openPanel.showsResizeIndicator = YES;

//should the user see hidden files (for user apps - usually no)
openPanel.showsHiddenFiles = NO;

//can the user select a directory?
openPanel.canChooseDirectories = NO;

//can the user create directories while using the dialogue?
openPanel.canCreateDirectories = YES;

//should the user be able to select multiple files?
openPanel.allowsMultipleSelection = NO;

//an array of file extensions to filter the file list
openPanel.allowedFileTypes = @[@"ped"];

//this launches the dialogue
[openPanel beginSheetModalForWindow:appDelegate.controlsWindow
                completionHandler:^(NSInteger result) {

                    //if the result is NSOKButton
                    //the user selected a file
                    if (result==NSOKButton) {

                        //get the selected file URLs
                        NSURL *selection = openPanel.URLs[0];

                        //finally store the selected file path as a string
                        NSString* path = [[selection path] stringByResolvingSymlinksInPath];

                        //here add yuor own code to open the file

                    }

                }];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文