简体   繁体   English

如何以编程方式将日期从UIDatePicker作为字符串传递到另一个View Controller中的UITableView的titleForHeaderInSection?

[英]How to pass a date from UIDatePicker as a string to a UITableView's titleForHeaderInSection in another View Controller programmatically?

Right now in my TableViewController.m I have a UITableView with a date that was converted into a string for the titleForHeaderInSection. 现在在我的TableViewController.m中,我有一个UITableView,其日期已转换为titleForHeaderInSection的字符串。 This string is known as the gamePlayedOnDateDisplayString and is set when a user creates a Game. 该字符串称为gamePlayedOnDateDisplayString,在用户创建游戏时设置。 The string is set as the date that the particular "Game" was created. 该字符串设置为创建特定“游戏”的日期。 I have written the logic to this string in a file that is an extension of the Game model. 我已经将逻辑写入该字符串到Game模型的扩展文件中。

I would like to implement a feature where a user can select a particular Game and change the date using a UIDatePicker. 我想实现一个功能,用户可以使用UIDatePicker选择特定的游戏并更改日期。 I have created a new view controller called UIDatePickerViewController to display my UIDatePicker. 我创建了一个新的名为UIDatePickerViewController的视图控制器,以显示我的UIDatePicker。 When a user selects a date on the picker and hits "DONE," I would like that date to update the gamePlayedOnDateDisplayString. 当用户在选择器上选择一个日期并单击“完成”时,我希望该日期更新gamePlayedOnDateDisplayString。

My initial thoughts were to use a delegate. 我最初的想法是使用一名代表。 For example when a user hits "DONE" on my UIDatePickerViewController it communicates with my TableViewController.m to update the titleForHeaderInSection, but I got a little lost when trying to implement that. 例如,当用户在我的UIDatePickerViewController上单击“ DONE”时,它将与TableViewController.m通信以更新titleForHeaderInSection,但是在尝试实现该功能时我有些失落。 I am hoping that one of you could point me in the right direction. 我希望你们中的一个能指出正确的方向。 I hope this is enough information. 我希望这是足够的信息。

Thank you. 谢谢。

A view controller with a date picker is a perfectly good -- probably best -- approach to edit the date. 具有日期选择器的视图控制器是一种很好的(可能是最好的)编辑日期的方法。 So too, is the delegate an acceptable -- but maybe not the best -- way to communicate the date change back to the table vc. 因此,将日期更改传达回表vc的委托也是一种可接受的方法,但可能不是最佳方法。

Remember a couple ideas: the point of these vcs is to present and edit aspect of a model . 请记住一些想法:这些vcs的目的是介绍和编辑模型的各个方面。 A very good pattern that uses no delegate is this: 一个不使用委托的非常好的模式是:

  1. on the table vc's prepare for segue, set the game which will be edited as a property on the view controller about to be presented (don't think of it as a DateSettingViewController, but as a GameEditingViewController) 在VC的准备表上,设置将要编辑的游戏,作为将要呈现的视图控制器上的属性(不要将其视为DateSettingViewController,而应将其视为GameEditingViewController)

  2. That GameEditingViewController, upon the user selecting a date should set the picked date on the game that it is editing. 用户选择日期后,该GameEditingViewController应该在正在编辑的游戏上设置选择日期。

  3. Here's the happy, non-delegate part: the table vc should reload its data upon appearing. 这是快乐的,非授权的部分:表vc在出现时应重新加载其数据。 It has, presumably, an array of games, and upon reappearing, one of those games now has an edited date property, which will be evident when it's corresponding table cell is reloaded. 据推测,它具有一系列游戏,并且在重新出现时,其中一个游戏现在具有已编辑的date属性,当重新加载相应的表格单元格时,该属性将很明显。

  4. (more of an incidental point, but) An NSDate is a richer and more capable object than a string that merely describes a date in a particular locale. (更多是偶然的,但是)NSDate是一个比仅在特定语言环境中描述日期的字符串更丰富,功能更强大的对象。 Try to avoid passing (and storing) string representations of dates. 尝试避免传递(和存储)日期的字符串表示形式。 Wherever possible, make the NSDate the source of truth, converting into a string only when a human needs to see it, like when configuring a cell. 尽可能使NSDate成为真相的来源,仅在人类需要看到它时才将其转换为字符串,例如在配置单元格时。

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

相关问题 如何从另一个View Controller更新UITableView? - How to update UITableView from another View Controller? NSFetchedResultsController用于从coredata获取日期并在UITableview的titleForHeaderInSection中显示日期 - NSFetchedResultsController for fetching date from coredata and display the date in titleForHeaderInSection of UITableview 以编程方式将数据从 UITableView 推送到查看控制器 - push data from UITableView to view controller programmatically 初学者:如何从另一个视图以编程方式呈现UITableView - Beginner: How to present UITableView programmatically from another view 有关UITableView的分组节及其titleForHeaderInSection如何工作的说明? - Explanation on how UITableView's grouped sections and their titleForHeaderInSection work? 如何在TitleForHeaderInSection上对日期进行排序 - How to Sort Date on TitleForHeaderInSection 如何在 Swift 中以编程方式从一个控制器导航到另一个视图控制器? - How to navigate from one controller to another View Controller programmatically in Swift? 如何从另一个视图将标准特定或过滤的数据源传递给UITableview? - how to pass a criteria specific or filtered datasource to UITableview from another view? 如何将集合视图单元格的indexPath传递给另一个视图控制器 - How to pass a collection view cell's indexPath to another view controller 从字符串设置uidatepicker日期 - set uidatepicker date from string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM