【问题标题】:UILabel won't update even on dispatch_async即使在 dispatch_async 上,UILabel 也不会更新
【发布时间】:2013-06-08 14:04:10
【问题描述】:

我的标签不会更新的任何原因?

- (void)setLabel:(NSNotification*)notification {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"setLabel");
        [self.label setText:@"My Label"];
    });
}

我也尝试过使用 performSelectorOnMainThread 无济于事。

请注意,setLabel 出现在日志中。

附加信息:

我还有另外两个功能,它们做同样的事情,但只是使用不同的文本。其他两个函数没有 dispatch_async 但它们都可以工作。此外,两个工作函数的通知是由 NSURLConnection 发送的(this post 中的方法 #2)。而上述非工作功能的通知是通过调用 FBRequestConnection 发送的(请参阅this post)。

为了清楚起见,我的另外两个工作函数如下:

- (void)setLabel2:(NSNotification*)notification {
    NSLog(@"setLabel2");
    [self.label setText:@"My Label 2"];
}    
- (void)setLabel3:(NSNotification*)notification {
    NSLog(@"setLabel3");
    [self.label setText:@"My Label 3"];
}

是的,我确实尝试在我的代码中删除 dispatch_async。其实原本没有dispatch_async是因为另外两个都在工作。

【问题讨论】:

  • 您是否尝试过暂时移除 dispatch_async 以查看是否设置了标签?
  • 确保您使用正确的标签并将其设置为您故事板的正确出口
  • @Kreiri - 如果它是 nil,它不应该让应用程序崩溃吗?拨打该线路后,我的不会崩溃。
  • @Jfuellert - 它最初没有 dispatch_async,我添加它以确保它更新但它没有。
  • nil 发送消息在Objective-C 中是合法的。它什么也不做。你需要弄清楚为什么self.label 为零。

标签: ios uilabel grand-central-dispatch performselector dispatch-async


【解决方案1】:

如果您在设置文本并打印 self.label 的描述之后立即设置断点,是否显示文本已更改?如果是这样,它必须在此方法触发后在其他地方重置。如果 self.label 为 nil 那就是你的问题

【讨论】:

  • “什么都没发生”的最常见原因是nil。那是我首先要看的地方。
  • @anders - 不,即使步入代码也不会设置文本。
  • @Rob Napier - 如果它是 nil,它不应该让应用程序崩溃吗?拨打该线路后,我的不会崩溃。
  • 不,发送到 nil 的消息不会导致崩溃。
  • @Catfish_Man - 不幸的是它不是零。 :(
猜你喜欢
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 2020-12-07
  • 1970-01-01
  • 2020-01-16
  • 2022-01-12
  • 2021-07-26
相关资源
最近更新 更多