Objective C 多线程 NSWindow 替代方案?

Objective C Multi thread NSWindow alternative?

我在做的时候老是崩溃:

[NSWindow orderFront:nil]

来自我在我的应用程序中生成的线程。是否无法像 GTK+ 那样使用来自线程的 UI 元素?

编辑: 哦,天哪,刚刚发现这个:

所以显然我不能从另一个线程使用 NSWindow,所以 objc 已经出来了,是否可以用 CoreFoundation 来做多线程 window 的东西?我必须从线程开始,所以我正在寻找替代方法

UI交互总是要在主线程上完成。

您可以简单地在主线程上使用 GCD 分派有问题的代码:

dispatch_async( dispatch_get_main_queue(), ^(void)
{
   [NSWindow orderFront:nil];
});

您只能在主线程上使用 UI 个元素。

我使用 GCD 来确保所有 UI 活动都 运行 在正确的线程上:

dispatch_sync(dispatch_get_main_queue(), ^{
    // Do your UI updates!
});

why:

In Cocoa Touch, the UIApplication i.e. the instance of your application is attached to the main thread because this thread is created by UIApplicatioMain(), the entry point function of Cocoa Touch. It sets up main event loop, including the application’s run loop, and begins processing events. Application's main event loop receives all the UI events i.e. touch, gestures etc.