目录:
1. 存文件
2. 取文件
3. 按类型存文件
1.存文件
NSSavePanel* panel = [NSSavePanel savePanel];
//设置默认的文件名
[panel setNameFieldStringValue:@"213.html"];
//设置提示信息
[panel setMessage:@"将网页存到哪里?"];
//
[panel setAllowsOtherFileTypes:YES];
//设置允许的文件类型
[panel setAllowedFileTypes:@[@"html"]];
//隐藏扩展名
[panel setExtensionHidden:YES];
//设置可以创建文件
[panel setCanCreateDirectories:YES];
//运行窗口
[panel beginSheetModalForWindow:self.view.window
completionHandler:^(NSInteger result){
if (result == NSFileHandlingPanelOKButton)
{
NSString *path = [[panel URL] path];
//将文字存入,保存成HTML
[@"onecodego" writeToFile:path atomically:YES
encoding:NSUTF8StringEncoding error:nil];
}
}];
屏幕快照 2016-09-01 上午11.00.46.png
2.取文件
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
//设置默认路径
//[openDlg setDirectory:NSOpenStepRootDirectory()];
[openDlg setCanChooseFiles:TRUE];
//设置可以打开文件夹
[openDlg setCanChooseDirectories:FALSE];
//设置是否可多选
[openDlg setAllowsMultipleSelection:TRUE];
[openDlg setAllowsOtherFileTypes:FALSE];
//允许的文件类型
[openDlg setAllowedFileTypes:@[@"png", @"PNG"]];
if ([openDlg runModal] == NSModalResponseOK)
{
//用户选中的路径
NSString* fileNameOpened = [[[openDlg URLs] objectAtIndex:0] path];
NSLog(@"%@",fileNameOpened);
}
屏幕快照 2016-09-01 上午11.29.52.png
3. 按类型存文件
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 = @"Image type: ";
NSComboBox *Ext = [[NSComboBox alloc] initWithFrame:NSMakeRect(80, 8, 100, 25)];
[Ext addItemsWithObjectValues:@[@".bmp", @".jpg", @".png", @".tif"]];
//默认选择第几个
[Ext selectItemAtIndex:0];
[viewExt addSubview:labExt];
[viewExt addSubview: Ext];
[panel setAccessoryView:viewExt];
[panel runModal];
屏幕快照 2016-09-01 下午12.04.02.png