简体   繁体   English

为什么我的标签文本不使用NSDateFormatter stringFromDate更改?

[英]Why doesn't my label text change using NSDateFormatter stringFromDate?

I've got a simple little test program --two labels and two buttons. 我有一个简单的小测试程序-两个标签和两个按钮。 When you hit either of the two buttons, the top label displays the currentTitle property of the appropriate button. 当您单击两个按钮中的任何一个时,顶部标签将显示相应按钮的currentTitle属性。 The second label should display the time the button was tapped, but it comes up blank after erasing the original "Label" text. 第二个标签应显示点击按钮的时间,但在删除原始的“标签”文本后,该标签将变为空白。

The two NSLog statements work as expected. 这两个NSLog语句按预期工作。

- (IBAction)buttonHit:(id)sender
{
    DataClass *dataObject = [[DataClass alloc] init];
    UIButton *resultButton = (UIButton *)sender;
    dataObject.timeTapped = NSDate.date;
    dataObject.nameTapped = resultButton.currentTitle;

    self.nameLabel.text = dataObject.nameTapped;
    NSString *timeOfHit = [[NSString alloc] init];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    timeOfHit = [formatter stringFromDate:dataObject.timeTapped];
    self.timeLabel.text = timeOfHit;


    NSLog(@"Button pressed was %@", dataObject.nameTapped);
    NSLog(@"Time pressed was %@", dataObject.timeTapped);

}

All help appreciated! 所有帮助表示赞赏!

--Tim -蒂姆

The reason of blank maybe is that you have not set the formatter's property. 空白的原因可能是您尚未设置格式化程序的属性。 You can add one line code : 您可以添加一个行代码:

formatter.dateFormat = @"yyyy-MM-dd HH-mm-ss";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM