简体   繁体   中英

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. This string is known as the gamePlayedOnDateDisplayString and is set when a user creates a Game. 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.

I would like to implement a feature where a user can select a particular Game and change the date using a UIDatePicker. I have created a new view controller called UIDatePickerViewController to display my UIDatePicker. When a user selects a date on the picker and hits "DONE," I would like that date to update the 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. 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.

Remember a couple ideas: the point of these vcs is to present and edit aspect of a model . 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)

  2. That GameEditingViewController, upon the user selecting a date should set the picked date on the game that it is editing.

  3. Here's the happy, non-delegate part: the table vc should reload its data upon appearing. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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