0

我正在尝试为 OS X 构建一个基本的动态库,它只显示一个用于打开文件的对话框。我的代码看起来像这样:

NSOpenPanel * dlg = [NSOpenPanel openPanel];
...//setting title and other properties for dlg

dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_main_queue(), ^
{
    resButton = [dlg runModal];
});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
if (resButton == NSFileHandlingPanelOKButton)//resButton is global
{...}

现在虽然这基本上可行,但一切都有点偏离:

-对话框永远不会以相同的方式初始化两次(不同的初始目录,布局模式......)。

- 有时初始目录显示为空,直到我选择另一个并再次选择第一个。

-“右键单击”菜单不会显示。

- 滚动反弹效果不起作用(!!!)。我可以无限向下滚动,直到一切都消失。

- 在列模式下,预览不起作用(加载图标永远转动),尽管在大图标模式下查看时,图像有正确的预览。

就像有一个完整的更新线程没有运行。它可能与调用 lib 的奇怪上下文有关:来自使用 JNA 的 java 程序。但我希望也许有人知道一个可以解决问题的小技巧,比如“只需调用 [system startUpdateTask]”或其他东西 :)

谢谢你的帮助

4

1 回答 1

1

(评论回复后:)

你可以尝试的东西(我无法测试你的场景)。NSOpenPanel/NSSavePanel是自从引入沙盒以来非常微妙的类,需要小心处理。

正如您所发现的,所有 UI 操作都需要在主线程上执行。但是,不要使用dispatch_*函数,而是尝试使用同步performSelectorOnMainThread

NSOpenPanel * dlg = [NSOpenPanel openPanel];
... //setting title and other properties for dlg

resButton = [dlg performSelectorOnMainThread:@selector(runModal)
                                  withObject:nil
                               waitUntilDone:YES];

if (resButton == NSFileHandlingPanelOKButton) //resButton is global
{...}

这可能会解决您的问题,也可能不会...

附录

我的错,正如您正确指出的那样,performSelectorOnMainThread它没有返回值。您可以改为:

resButton作为实例变量添加到您的类中。

添加方法:

- (void) myRunModal:(NSOpenPanel *)dlg
{
   resButton = [dlg runModal];
}

将代码更改为:

[self performSelectorOnMainThread:@selector(myRunModal:)
                       withObject:dlg
                    waitUntilDone:YES];

或类似的东西。

于 2013-10-10T21:08:53.097 回答