当我使用时
NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"pdf", @"PDF", nil];
NSInteger result = [openPanel runModalForTypes:fileTypes];
它按预期工作(只能选择pdf文件),但是当我使用时
NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"pdf", @"PDF", nil];
[openPanel setAllowedFileTypes:fileTypes];
NSInteger result = [openPanel runModal];
我可以选择所有类型的文件,而不仅仅是 pdf。文档说 runModalForTypes 已被弃用,我们应该使用第二种方式。我做错了什么吗?
最佳答案
您正在为 Mac OS X 10.6 及更高版本进行构建吗?根据头文件:“在低于 10.6 的版本上,此属性将被忽略。”
否则,您的代码看起来是正确的(假设您在某个时候发布了 fileTypes
)并且适用于我(在 Mac OS X 10.7.2 上测试)。
一个小建议是使用统一类型标识符来识别 PDF,而不是硬编码文件扩展名,如下所示:
NSArray *fileTypes = [NSArray arrayWithObjects:(id)kUTTypePDF, nil];
关于macos - runModalForTypes 有效,但 setAllowedFileTypes+runModal 无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8588549/