循环刷新窗口

发布于 2024-12-26 10:20:21 字数 127 浏览 3 评论 0原文

我有一个在 Mac 上用 xcode/cocoa 编写的应用程序。 主窗口上的标签在每次出现 [label setStringValue] 的重循环时都会更改,但仅在循环结束时刷新。 我如何才能在每次出现时刷新它?

谢谢 !

I have an application written in xcode/cocoa on Mac.
A label on the main window is changed in every occurrence of a heavy loop with [label setStringValue], however it is refreshed only at the end of the loop.
How can I have it refreshed in each occurrence ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏林苍穹下 2025-01-02 10:20:22

你应该使用队列。 backgroundQueue 中的重循环和 mainQueue 中的 [label setStringValue] 。

例子:

dispatch_queue_t backgroundQueue = 
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_queue_t mainQueue = dispatch_get_main_queue();


dispatch_async(backgroundQueue,^{
//Your loop

    dispatch_async(mainQueue,^{
    //Set Label value
    });

});

You should use a queue. Your heavy loop in backgroundQueue and [label setStringValue] in mainQueue.

Example:

dispatch_queue_t backgroundQueue = 
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_queue_t mainQueue = dispatch_get_main_queue();


dispatch_async(backgroundQueue,^{
//Your loop

    dispatch_async(mainQueue,^{
    //Set Label value
    });

});
囍孤女 2025-01-02 10:20:22

您的问题是您在主线程上完成工作(循环)。主线程负责更新UI,不能被阻塞!

您需要启动一个新线程来完成繁重的工作并更新主线程上的 UI。

您应该看看 GCD,这是一个很好的轻量级解决方案,或者看看 PerformSelector... 方法。

Your problem is that you do the work (the loop) on the main thread. The main thread is responsible for updating the UI and must not be blocked!

You need to start a new thread to do the heavy work and update your UI on the main thread.

You should have a look at GCD which is a good an lightweight solution for that or have a look at the performSelector... methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文