标题
[macos/cocoa/appkit]原生的打开文件对话框代码如何写
clq
浏览(338) +
2023-09-23 14:32:58 发表
编辑
关键字:
[2023-12-02 14:42:39 最后更新]
[macos/cocoa]原生的打开文件对话框代码如何写
答案就是用 cocoa ,其实以 cocoa 为关键字的一部分就可以搜索到好多内容。只不过没开发过原生的人不知道 cocoa 这个关键字而已。
--------------------------------------------------------
打开文件选择对话框:
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:YES]; //设置多选模式
[openPanel setAllowedFileTypes:[NSImage imageFileTypes]];//设置文件的默认类型
[openPanel setMessage:@"Please select image(s) to show."];
[openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton)
{
NSArray *paths = [openPanel filenames];
/*如果上面不设置的多选模式,则默认情况下是单选的,可以
NSString * path = [openPanel filename];*/
//dosomething.......................
}
}];
--------------------------------------------------------
- (IBAction)OnBT_OpenSaveDialog:(id)sender
{
// File : no 扩展名
// NSSavePanel *savePanel = [NSSavePanel savePanel];
// [savePanel runModal];
NSSavePanel* panel = [NSSavePanel savePanel];
NSView *viewExt = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 180, 40)];
NSTextField *labExt = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 10, 80, 20)];
[labExt setBordered:NO];
[labExt setDrawsBackground:NO];
labExt.stringValue = @"File type: ";
NSComboBox *cbExt = [[NSComboBox alloc] initWithFrame:NSMakeRect(80, 8, 100, 25)];
//[cbExt addItemsWithObjectValues:@[@".bmp", @".jpg", @".png", @".tif"]];
[cbExt addItemsWithObjectValues:@[@".txt"]];
cbExt.stringValue = @".txt";
[viewExt addSubview:labExt];
[viewExt addSubview:cbExt];
[panel setAccessoryView:viewExt];
NSInteger result = [panel runModal];
if (result == NSFileHandlingPanelOKButton)
{
NSString *path = [[panel URL] path];
NSLog(path);
}
}
//ok!
- (IBAction)OnBT_OpenSelectedDialog:(id)sender
{
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setMessage:@""];
[panel setPrompt:@"OK"];
[panel setCanChooseDirectories:YES];
[panel setCanCreateDirectories:YES];
[panel setCanChooseFiles:YES];
NSString *path_all;
NSInteger result = [panel runModal];
if (result == NSFileHandlingPanelOKButton)
{
path_all = [[panel URL] path];
NSLog(path_all);
}
}
test OK!
--------------------------------------------------------
lazarus 中对应的源码为
/usr/local/share/fpcsrc/3.0.4/packages/cocoaint/src/appkit/NSOpenPanel.inc
{ Parsed from Appkit.framework NSOpenPanel.h }
可惜国内对 fpc 相关扩展的语法资料太少,很多部分看不懂。还搞不清楚是编译器内置的还调用的动态库/静态库。
clq
2023-11-20 19:04:42 发表
编辑
#import <Cocoa/Cocoa.h> //clq add for mac
#import <Foundation/Foundation.h> //clq add for mac
#import <AppKit/NSSavePanel.h>
#import <AppKit/NSOpenPanel.h>
NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.