【发布时间】:2023-03-23 02:30:01
【问题描述】:
我正在通过此代码选择文件:
- (IBAction)selectFile:(id)sender {
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setPrompt:@"Select"];
fileTypes = [NSArray arrayWithObjects:@"wmv", @"3gp", @"mp4", @"avi", @"mp3", @"mma", @"wav", @"jpeg", @"png", @"jpg", @"tiff", nil];
// NSArray *JpegfileTypes = [NSArray arrayWithObjects:@"jpeg", @"png", @"jpg", @"tiff", @"mp3" nil];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
//Enable multiple selection of files
[openDlg setAllowsMultipleSelection:YES];
// Enable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:YES];
// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModalForDirectory:nil file:nil types:fileTypes] == NSOKButton )
{
// Get an array containing the full filenames of all
// files and directories selected.
files = [[openDlg filenames] retain];
int i; // Loop counter.
// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ )
{
NSString *tempFilePath = [files objectAtIndex:i];
NSLog(@"tempFilePath::: %@",tempFilePath);
inputFilePath = [[files objectAtIndex:i] retain];
NSLog(@"filename::: %@", inputFilePath);
// Do something with the filename.
[selectedFile setStringValue:inputFilePath];
NSLog(@"selectedFile:::: %@", selectedFile);
}
}
}
然后在选择之后我使用这个代码来处理选择的文件。
- (IBAction)setMessage:(id)sender {
[fileGenProgress startAnimation:self];
NSString *message = [[NSString alloc] initWithFormat:@"Started"];
[lblMessage setStringValue:message];
[message release];
[self startProcessingVideoFile];
[self startProcessingAudioFile];
[self startProcessingJpg];
}
我面临的问题是,如果所选文件是 3gp/mp4 或 jpg 或 mp3,我不知道如何比较不同的字符串。就像用户选择了一些视频文件一样,[self startProcessingVideoFile]; 方法将运行,如果他选择了一些 JPG 或 PNG 等文件,则[self startProcessingAudioFile]; 方法将运行。
选择的路径不仅是文件的扩展名。那么在这种情况下如何强制- (IBAction)setMessage:(id)sender 方法运行适当的方法。
【问题讨论】:
标签: objective-c xcode cocoa xcode4.2