user1754527
user1754527

Reputation:

How to set the current date as a title

I am making a simple iOS notes application just to learn, and I wanted to add a simple date when the notes are created. Kinda like the default notes.app. This is what I have tried so far:

I have created a label in the storyboard and made it into a property and synthesized it. This is my code

- (void)configureCell:(IdeasTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

  ...

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
 NSDate *dateTmp;
cell.dateLabel.text = [dateFormat stringFromDate:dateTmp];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

It keeps returning an error... any ideas on what is causing the problem? Sorry, for the dumb question, but I have just started iOS dev.

Upvotes: 3

Views: 111

Answers (2)

Vinodh
Vinodh

Reputation: 5268

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateTmp = [NSDate date];
cell.detailTextLabel.text = [dateFormat stringFromDate:dateTmp];

the above code gives me the proper result

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726589

The reason you get a crash is that the NSDate* object is not initialized. You need to assign it an instance representing the current date:

NSDate *dateTmp = [NSDate date];

Upvotes: 2

Related Questions