문제

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top