UILabel won't update even on dispatch_async

て烟熏妆下的殇ゞ 提交于 2019-12-13 08:36:09

问题


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.

Note that setLabel is appears on the log.

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. Also, the notification of the two working function was sent by NSURLConnection (method #2 in this post). While the notification of the non working function above, was sent by a call to FBRequestConnection (see this post).

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. In fact, originally there was no dispatch_async because the other two were working.


回答1:


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? If so it must be getting reset somewhere else after this method fires. If self.label is nil than there's your problem



来源:https://stackoverflow.com/questions/17000283/uilabel-wont-update-even-on-dispatch-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!