NSOpenPanel *openDlg = [NSOpenPanelopenPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:YES];
[openDlg setPrompt:@"Select"];
if([openDlgrunModalForDirectory:@"/AppleInternal/Applications"file:@"myAppName.app"] ==NSOKButton)
{
NSArray *files = [openDlgfilenames];
for(int i =0; i < [files count]; i++){
NSString *fileName = [filesobjectAtIndex:i];
NSLog(@"Select File Name : %@",fileName);
[pathControl setURL:[NSURLfileURLWithPath:fileName]]; //NSPathControl *pathControl
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setCanChooseFiles:YES];
[op setCanChooseDirectories:YES];
[op beginSheetForDirectory: [[pathSelect URL]path]
file: nil
types: nil
modalForWindow: [self window]
modalDelegate: self
didEndSelector: @selector(openPanelDidEnd:returnCode:contextInfo:)
contextInfo: nil
];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- (void) openPanelDidEnd:(NSOpenPanel *)op
returnCode:(int)returnCode
contextInfo:(void *)ci
{
if (returnCode == NSOKButton) {
//NSString* path = [op filename];
NSArray *files = [op filenames];
for(int i = 0; i < [files count]; i++){
NSString *fileName = [files objectAtIndex:i];
NSLog(@"Selsct File Name : %@",fileName);
[pathSelect setURL:[NSURL fileURLWithPath:fileName]];
}
}
}
本文介绍了如何使用Objective-C中的NSOpenPanel来实现文件选择功能。通过设置对话框选项允许选择文件和目录,并通过runModalForDirectory方法启动对话框。文章还展示了如何处理选择结果并更新UI。

5527

被折叠的 条评论
为什么被折叠?



