【问题标题】:Unable to display value on UILabel upon button click按钮单击时无法在 UILabel 上显示值
【发布时间】: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


【解决方案1】:
  1. 首先创建一个名为 WetherDetailVC 的新类
  2. 现在您有两个文件 .h 和 .m
  3. 在身份检查器中将 WetherDetailVC 类分配给您的视图控制器
  4. 如果您不想再次创建插座,只需剪切所有插座(maxTempField、minTempField、...等)并将其全部粘贴到 WetherDetailVC.h 文件中
  5. 现在使用 ViewController.h 中的类对象访问所有插座

    - (IBAction)moreProperties:(id)sender {
     UIStoryboard *refToStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
     WetherDetail *pointerToMoreValuesVC = [refToStoryBoard instantiateViewControllerWithIdentifier:@"MorePropertiesVC"];
     pointerToMoreValuesVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:pointerToMoreValuesVC animated:YES completion:nil];
     pointerToMoreValuesVC.noteLabel.text = @"Random Text";
     pointerToMoreValuesVC.maxTempField.text = [NSString stringWithFormat:@"%@",vari[@"main"][@"temp_max"]];
     pointerToMoreValuesVC.atmosphericPressureField.text = [NSString stringWithFormat:@"%@", vari[@"main"][@"pressure"]];
      pointerToMoreValuesVC.windSpeedField.text = [NSString stringWithFormat:@"%@", vari[@"wind"][@"speed"]];
    }
    

【讨论】:

  • 为故事板上的新视图控制器创建一个新类解决了整个问题。效果很好。谢谢!
猜你喜欢
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 2022-08-02
  • 2019-12-14
  • 1970-01-01
  • 2017-08-19
  • 1970-01-01
  • 2021-04-07
相关资源
最近更新 更多