简体   繁体   English

UILabel即使在dispatch_async上也不会更新

[英]UILabel won't update even on dispatch_async

Any reason why my label won't update? 为什么我的标签不会更新?

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

I've also tried using performSelectorOnMainThread to no avail. 我也试过使用performSelectorOnMainThread无济于事。

Note that setLabel is appears on the log. 请注意,setLabel出现在日志中。

Additional info: 附加信息:

I also have two other functions which does the same thing but only with a different text. 我还有另外两个函数,它们执行相同的操作,但仅使用不同的文本。 The two other functions doesn't have dispatch_async but they both work. 另外两个函数没有dispatch_async,但是它们都可以工作。 Also, the notification of the two working function was sent by NSURLConnection (method #2 in this post ). 另外,两个工作功能的通知,通过NSURLConnection的(方法#2中发送此信息 )。 While the notification of the non working function above, was sent by a call to FBRequestConnection (see this post ). 虽然上面的非工作功能的通知是通过调用FBRequestConnection发送的(请参阅此文章 )。

For clarity, my two other working functions is as follows: 为了清楚起见,我的其他两个工作功能如下:

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

Yes I did try to remove dispatch_async in my code. 是的,我确实尝试删除了代码中的dispatch_async。 In fact, originally there was no dispatch_async because the other two were working. 实际上,最初没有dispatch_async,因为其他两个都在工作。

If you set a break point right after where you set the text and print description of self.label does it show the text has changed? 如果您在设置文本并打印self.label的描述之后立即设置断点,是否表明文本已更改? If so it must be getting reset somewhere else after this method fires. 如果是这样,则在触发此方法后必须将其重置为其他位置。 If self.label is nil than there's your problem 如果self.label为nil,那是您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM