runModalForTypes works but setAllowedFileTypes+runModal doesn't?
-
23-03-2021 - |
题
When I use
NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"pdf", @"PDF", nil];
NSInteger result = [openPanel runModalForTypes:fileTypes];
it works as expected (only pdf files can be selected), but when I use
NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"pdf", @"PDF", nil];
[openPanel setAllowedFileTypes:fileTypes];
NSInteger result = [openPanel runModal];
I can select all types of files, not just pdfs. The documentation says that runModalForTypes is deprecated and we should use the second way. Am I doing something wrong?
解决方案
Are you building for Mac OS X 10.6 and later? According to the header file: "On versions less than 10.6, this property is ignored."
Otherwise, your code looks correct (assuming you release fileTypes
at some point) and works for me (tested on Mac OS X 10.7.2).
One minor suggestion is to use a Uniform Type Identifier to identify PDFs instead of hard-coding file extensions, like so:
NSArray *fileTypes = [NSArray arrayWithObjects:(id)kUTTypePDF, nil];
不隶属于 StackOverflow