我试图为OS构建一个基本的动态库,它只显示一个打开文件的对话框。我的代码如下所示:
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
{...}现在,虽然这基本上是可行的,但是每件事都有点不对劲:
-The对话框从来不会以相同的方式初始化两次(不同的初始目录、布局模式.)。
-Sometimes初始目录显示为空,直到我再次选择另一个目录和第一个目录。
-“右击”菜单不会显示。
-Scrolling反弹效应不起作用(!)我可以无限期地向下滚动,直到一切都消失。
-In列模式,预览不工作(加载图标永远转),虽然在大图标模式下,图像有其适当的预览。
好像有一个完整的更新线程没有运行。它可能链接到调用lib的奇怪上下文:来自使用JNA的java程序。但我希望也许有人知道一个可以解决问题的小窍门,比如“调用系统startUpdateTask”之类的:)
谢谢你的帮助
发布于 2013-10-10 21:08:53
(答复后:)
一些你可以尝试的东西(我不能测试你的场景)。自沙箱引入以来,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];或者类似的东西。
https://stackoverflow.com/questions/19298777
复制相似问题