Cocoa UI 元素未更新

发布于 2024-08-30 10:57:31 字数 754 浏览 3 评论 0原文

我有一些 Cocoa UI 元素,它们与 NSView 对象中实例化的对象有出口连接,而该对象又由 NSViewController 放置在那里。这些元素(明确的进度条和文本标签)不会更新:尽管其值不断变化,但进度条是死的和空的,文本标签不会通过 [textLabel setHidden:NO] 取消隐藏,文本标签不会更改它的字符串。

我所知道的是:

  • 绑定值和在代码中设置它们之间没有区别。无论哪种方式都没有改变。
  • 我检查过插座连接。他们都在那里。
  • 我尝试过 [X displayIfNeeded],其中 X 是 UI 对象本身、包含的 NSView 和主窗口。没有区别。
  • [progressBar setUsesThreadedAnimation:YES] 没有区别。有趣的是,如果我在程序中查看进度条,_threadedAnimation 仍然是“否”。
  • 保存所有这些出口并执行导入操作的对象位于 NSViewController 对象拥有的 NSOperationQueue 中。

谢谢!

编辑:按照建议,我调用了 [self PerformSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithInt:myObject] waitUntilDone:NO]。 (我也尝试过 waitUntilDone:YES。)它仍然没有更新。调试器清楚地显示 updateProgress: 发生在主线程中,所以我不知道缺少什么。

I have a few Cocoa UI elements with outlet connexions to an object instantiated within an NSView object, which is in turn put there by an NSViewController. These elements, a definite progress bar and a text label, are not updating: the progress bar is dead and empty despite having its value change constantly, the text label does not unhide through [textLabel setHidden:NO], the text label does not change its string.

What I know:

  • There's no difference between binding values and setting them in code. Nothing changes either way.
  • I've checked outlet connections. They're all there.
  • I've tried [X displayIfNeeded], where X has been the UI objects themselves, the containing NSView, and the main window. No difference.
  • [progressBar setUsesThreadedAnimation:YES] makes no difference. Interestingly, if I look at progressBar mid-program, _threadedAnimation is still NO.
  • The object holding all these outlets and performing an import operation is in an NSOperationQueue owned by the NSViewController object.

Thanks!

EDIT: As suggested, I called [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithInt:myObject] waitUntilDone:NO]. (I've also tried waitUntilDone:YES.) It's still not updating. The debugger clearly shows updateProgress: taking place in the main thread, so I don't know what's missing.

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

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

发布评论

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

评论(1

意中人 2024-09-06 10:57:31

所有 NSOperation 任务都在单独的线程中执行。不允许您从单独的线程修改 UI,因为 AppKit 不是线程安全的。您将需要重构代码,以便使用以下方法在主线程上执行任何修改用户界面的调用:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

All NSOperation tasks are performed in separate threads. You are not allowed to modify the UI from a separate thread as AppKit is not thread-safe. You will need to refactor your code so that any calls that modify the user interface are performed on the main thread using the method:

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