objective-c – 如何在xcode可可项目中创建一个打开的文件对话框?

前端之家收集整理的这篇文章主要介绍了objective-c – 如何在xcode可可项目中创建一个打开的文件对话框?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找相当于 Windows的GetOpenFileName函数.

解决方法

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

                    }

                }];

猜你在找的cocoa相关文章