[英]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:
一个不使用委托的非常好的模式是:
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)
That GameEditingViewController, upon the user selecting a date should set the picked date on the game that it is editing. 用户选择日期后,该GameEditingViewController应该在正在编辑的游戏上设置选择日期。
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属性,当重新加载相应的表格单元格时,该属性将很明显。
(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.