作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Mac OS X 应用程序,它实现了 -(void)application openFiles:
方法来响应应用程序图标上的拖动文件。
我在目标信息设置的文档类型部分有一个允许的文件类型列表,Finder 确实允许拖动,但是当 PDF 在拖动项目列表中时,我的委托(delegate)方法被调用两次:一次用于所有没有 PDF 的元素,一个单独用于 PDF。
这当然使我无法妥善处理这种情况。
任何人都可以帮助我或解释发生了什么事吗?谢谢
最佳答案
我在我的一个应用程序中看到过这种行为(通常是在一次拖动一大堆文件时)。在我的变通方法中,我没有直接从 application:openFiles:
打开文件,而是将它们排队,并在稍等片刻后打开排队的文件。类似于以下内容:
- (void) application:(NSApplication*)sender openFiles:(NSArray*)filenames
{
// I saw cases in which dragging a bunch of files onto the app
// actually called application:openFiles several times, resulting
// in more than one window, with the dragged files split amongst them.
// This is lame. So we queue them up and open them all at once later.
[self queueFilesForOpening:filenames];
[NSApp replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
}
- (void) queueFilesForOpening:(NSArray*)filenames
{
[self.filesToOpen addObjectsFromArray:filenames];
[self performSelector:@selector(openQueuedFiles) withObject:nil afterDelay:0.25];
}
- (void) openQueuedFiles
{
if( self.filesToOpen.count == 0 ) return;
[self makeNewWindowWithFiles:self.filesToOpen];
[self.filesToOpen removeAllObjects];
}
关于objective-c - 为什么 NSApplicationDelegate 方法 openFiles : is being called multiple times on a multiple drag to the dock icon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37623734/
我是一名优秀的程序员,十分优秀!