首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示错位按钮的NSAlert

显示错位按钮的NSAlert
EN

Stack Overflow用户
提问于 2012-06-20 10:11:06
回答 1查看 318关注 0票数 0

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

因为我不能阻止主线程,也不想将其显示为模式。我使用了下面的代码。

代码语言:javascript
复制
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;
}

你知道为什么会发生这种事吗?

EN

回答 1

Stack Overflow用户

发布于 2012-06-21 01:58:03

哇,这是一些严重混乱的代码。

  • 您是如何决定调用-layout是一个必要的好主意的?
  • AppKit不是线程安全的;您不应该从任何线程调用-beginModalSessionForWindow:,但是主

不能让您为它运行模式会话,或者接管它的呈现

相反,可以使用-runModal-beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:在主线程上直接调用NSAlert

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11111939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档