【发布时间】:2016-05-23 20:05:17
【问题描述】:
我正在尝试在 UILabels 上显示天气数据值(通过 API 调用获取),代码如下:
self.maxTempField.text = [NSString stringWithFormat:@"%@",vari[@"main"][@"\"temp_max\""]];
NSLog(@"Max Temp %@", vari[@"main"][@"temp_max"]);
self.atmosphericPresField.text = [NSString stringWithFormat:@"%@", vari[@"main"][@"pressure"]];
self.windSpeed.text = [NSString stringWithFormat:@"%@", vari[@"wind"][@"speed"]];
NSLog(@"Speeed of the wind %@", vari[@"wind"][@"speed"]);
我无法理解为什么没有更新标签。我还尝试用UITextField 替换UILabel,但问题仍然存在。以下是按钮操作的完整代码:
- (IBAction)moreProperties:(id)sender {
UIStoryboard *refToStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UIViewController *pointerToMoreValuesVC = [refToStoryBoard instantiateViewControllerWithIdentifier:@"MorePropertiesVC"];
pointerToMoreValuesVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:pointerToMoreValuesVC animated:YES completion:nil];
self.noteLabel.text = @"Random Text";
dispatch_async(dispatch_get_main_queue(), ^{
self.maxTempField.text = [NSString stringWithFormat:@"%@",vari[@"main"][@"\"temp_max\""]];
NSLog(@"Max Temp %@", vari[@"main"][@"temp_max"]);
self.atmosphericPresField.text = [NSString stringWithFormat:@"%@", vari[@"main"][@"pressure"]];
self.windSpeed.text = [NSString stringWithFormat:@"%@", vari[@"wind"][@"speed"]];
NSLog(@"Speeed of the wind %@", vari[@"wind"][@"speed"]);
});
}
我还检查了我所有的标签连接,它们都很好。
【问题讨论】:
-
IBAction将始终在主线程上运行;没有必要使用dispatch_async(dispatch_get_main_queue(), ...)。 -
@JoshCaswell 我删除了主线程的指令,字段仍然不会更新。
-
NSLog语句是否出现?在您尝试更新标签的行上放置一个断点。self.maxTempField是非零吗?self.windSpeed不为零吗? -
@rmaddy NSLog 语句执行。我不明白 UITextFields 是否可以是非零..
-
清理您的项目并从 window=>projects 的缓存数据中删除并再次运行它。
标签: ios objective-c