我正在编写一个自定义身份验证,它具有一些动态内容(当前以标签的形式)。我希望能够响应事件更新标签。

假设以下声明:

  • parent = SFAuthorizationPluginView (subclass)
    • view = NSView (subclass)
      • NSTextField *label = ...

我按如下方式编辑标签文本(注意:这可以从后台调度队列执行):
[label setStringValue:@"new text"];

我试图使用以下内容强制更新:

  • [label setNeedsDisplay:YES];
  • dispatch_async(dispatch_get_main_queue(), ^{ [label setNeedsDisplay:YES]; });
  • [label display];
  • dispatch_async(dispatch_get_main_queue(), ^{ [label display]; });
  • [[label cell] update];
  • dispatch_async(dispatch_get_main_queue(), ^{ [[label cell] update]; });
  • [[label cell] needsDisplay];
  • dispatch_async(dispatch_get_main_queue(), ^{ [[label cell] needsDisplay]; });
  • [view setNeedsDisplay:YES];
  • dispatch_async(dispatch_get_main_queue(), ^{ [view setNeedsDisplay:YES]; });
  • [self updateView];
  • dispatch_async(dispatch_get_main_queue(), ^{ [self updateView]; });
  • [self displayView]
  • dispatch_async(dispatch_get_main_queue(), ^{ [self displayView]; });
但是,我得到了没有动作和闪烁的灰色屏幕的混合(表示我导致了异常)。

任何想法? (注意:我会在尝试各种选项时编辑这个问题)

有帮助吗?

解决方案

我认为所有您需要做的就是将标签更新为如下所示:

dispatch_async(dispatch_get_main_queue(), ^{
 label.text = "Hello World!";
});
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top