简体   繁体   English

如何使用核心数据在详细文本标签中保存和显示日期选择器值?

[英]How can I save and display date picker value in detail text label using core data?

I have a simple reminder app using core data, with 2 entities, one for 'details' of the reminder and one for the 'date' of the reminder. 我有一个使用核心数据的简单提醒应用程序,其中包含2个实体,一个用于提醒的“详细信息”,另一个用于提醒的“日期”。 The user adds a reminder and picks a date and that is then stored in a table view of all of their reminders. 用户添加一个提醒并选择一个日期,然后将其存储在所有提醒的表视图中。

I have the 'details' entity value saving correctly from the text field into the label of the table view but I can't seem to get the date to save from the date picker and into the detail text label of the table view. 我已经将'details'实体值正确地从文本字段保存到了表格视图的标签中,但是我似乎无法从日期选择器和表格视图的明细文本标签中获取要保存的日期。

In the EnterReminderViewController.m: 在EnterReminderViewController.m中:

-(void)insertReminderDetails {

CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
ReminderEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"ReminderEntry" inManagedObjectContext:coreDataStack.managedObjectContext];

entry.details = self.reminderTextField.text;
entry.date =  self.datePicker;


[coreDataStack saveContext];

}

And in UpcomingRemindersTableViewController.m: 在UpcomingRemindersTableViewController.m中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView    dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

ReminderEntry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];

cell.textLabel.text = entry.details;

 cell.detailTextLabel.text = entry.date;

return cell;

} }

Do I need to convert the value to a string for this to work? 我需要将值转换为字符串才能工作吗? Also, do I need to create a separate fetch request for the date? 另外,我是否需要为该日期创建一个单独的提取请求?

In your first code snippet, your entry.date property appears to be a Core Data "date" attribute, which is equivalent to an NSDate in code. 在您的第一个代码段中, entry.date属性似乎是Core Data的“ date”属性,等效于代码中的NSDate You're attempting to assign a UIDatePicker to it. 您正在尝试为其分配一个UIDatePicker That's incorrect, because UIDatePicker is a UI control that lets the user select a date, it's not a date in itself. 这是不正确的,因为UIDatePicker是一个UI控件,允许用户选择日期,它本身不是日期。 You need to ask the date picker for its current date value-- probably self.datePicker.date in your code. 您需要向日期选择器询问其当前日期值-可能在代码中为self.datePicker.date

In your second code snippet, you appear to be assigning entry.date to a UILabel text attribute. 在第二个代码段中,您似乎正在将entry.date分配给UILabel文本属性。 That's also incompatible, because dates are not strings. 这也是不兼容的,因为日期不是字符串。 A simple fix would be to use entry.date.description , but that will use GMT rather than local time. 一个简单的解决方法是使用entry.date.description ,但这将使用GMT而不是本地时间。 To get more control over converting a date to a string, look into NSDateFormatter . 要获得更多将日期转换为字符串的控制权,请查看NSDateFormatter

您可以将属性值设置为要转换的日期,然后将日期选择器日期保存为init。我们必须将日期选择器日期作为NS Date而不是核心日期中的字符串。希望它将对您有所帮助。

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

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