我在应用程序中有一个按钮,点击时应该会打开一个对话框。用户然后选择一个文件夹,单击确定,然后该应用程序显示该文件夹中的PDF文件的数量。查找文件夹中PDF的数量
我下面实现了下面的代码。如何扫描文件夹中的PDF文件数量?
- (IBAction)selectPathButton:(NSButton *)sender {
// Loop counter.
int i;
// Create a File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
// Set array of file types
NSArray *fileTypesArray;
fileTypesArray = [NSArray arrayWithObjects:@"pdf", nil];
// Enable options in the dialog.
[openDlg setCanChooseFiles:YES];
[openDlg setAllowedFileTypes:fileTypesArray];
[openDlg setAllowsMultipleSelection:TRUE];
// Display the dialog box. If the OK pressed,
// process the files.
if ([openDlg runModal] == NSOKButton) {
// Gets list of all files selected
NSArray *files = [openDlg URLs];
// Loop through the files and process them.
for(i = 0; i < [files count]; i++) {
}
NSInteger payCount = [files count];
self.payStubCountLabel.stringValue = [NSString stringWithFormat:@"%ld", (long)payCount];
}
}
到目前为止,您的代码并不完全符合该问题。如果你想让用户选择一个文件夹,然后设置'[openDlg setCanChooseDirectories:YES]'和'[openDlg setCanChooseFiles:NO]'。还是你想让用户多选PDF文件? (因为这是你现在拥有的。) – 2013-02-21 21:55:22
好的。谢谢! – 2013-02-25 16:13:37
我希望用户选择一个文件夹,然后它会计算所选文件夹中的pdf文件数量。上面显示的代码是从另一个论坛帖子复制/粘贴的。 – 2013-02-25 16:23:26