objective-c - 为什么 NSApplicationDelegate 方法 openFiles : is being called multiple times on a multiple drag to the dock icon?

标签 objective-c xcode cocoa nsapplication nsapplication-delegate

我有一个 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/

相关文章:

objective-c - 连接外部键盘时支持命令、控制和箭头键

xcode - 具有不同轴刻度的核心绘图图中的两个 y 轴

ios - UITableViewCell Storyboard 注册依赖于初始 View Controller ?

objective-c - Objective C 中硬编码字节的奇怪结果

iphone - 使用 NSPredicates/KVC 获取兄弟键数组

ios - 推送通知,是否可以让推送通知在应用程序打开时也向下滑动?

ios - 在另一个上下文中更新核心数据实体

objective-c - 写入磁带驱动器有什么不同吗? (普通 C 和 Objective-C)

iphone - 如何使开关转移到另一个 View Controller ?

objective-c - CPTBarPlot 字段的面积?