问题
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