rattletrap99
rattletrap99

Reputation: 1479

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. 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.

- (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

Upvotes: 0

Views: 90

Answers (1)

Misheral
Misheral

Reputation: 46

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";

Upvotes: 3

Related Questions