我有一个显示NSAlert窗口的多线程OSX应用程序。大多数情况下,UI看起来很好,但有时它会因为错放按钮而破坏UI,这看起来非常丑陋。

因为我不能阻止主线程,也不想将其显示为模式。我使用了下面的代码。
NSAlert* alert = [NSAlert alertWithMessageText:title defaultButton:defaultBtn alternateButton:alterBtn otherButton:otherBtn informativeTextWithFormat:msg];
[alert setAlertStyle:style];
BOOL isMainThread = (dispatch_get_current_queue() == dispatch_get_main_queue());
if(isMainThread)
[alert layout];
else
{
dispatch_sync(dispatch_get_main_queue(), ^{
[alert layout];
});
}
NSModalSession session = [NSApp beginModalSessionForWindow:alert.window];
__block NSUInteger response;
for (;;) {
if(isMainThread)
{
response = [NSApp runModalSession:session];
}
else
{
dispatch_sync(dispatch_get_main_queue(), ^{
response = [NSApp runModalSession:session];
});
}
if(response != NSRunContinuesResponse)
break;
}你知道为什么会发生这种事吗?
发布于 2012-06-21 01:58:03
哇,这是一些严重混乱的代码。
-layout是一个必要的好主意的?-beginModalSessionForWindow:,但是主不能让您为它运行模式会话,或者接管它的呈现
相反,可以使用-runModal或-beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:在主线程上直接调用NSAlert
https://stackoverflow.com/questions/11111939
复制相似问题