1 if ([NSThread isMainThread]) 2 { 3 [self.downloadMapBtn setImage:[UIImage imageNamed:@"download_map.png"] forState:UIControlStateNormal]; 4 [self.downloadMapBtn setNeedsDisplay]; 5 } 6 else 7 { 8 dispatch_sync(dispatch_get_main_queue(), ^{ 9 //Update UI in UI thread here 10 [self.downloadMapBtn setImage:[UIImage imageNamed:@"download_map.png"] forState:UIControlStateNormal]; 11 [self.downloadMapBtn setNeedsDisplay]; 14 }); 15 }
本文提供了一个在主线程中更新UI的代码示例。通过判断当前是否为主线程,并据此选择直接更新UI或调度到主线程进行更新,确保了UI组件能正确地显示变化。
1637

被折叠的 条评论
为什么被折叠?



