UIDatePicker:有一个日期和时间选项,但如何在应用程序中显示这两个选项



编辑:为了将来的参考:为NSDateFormatter找到了这个。此文档显示了不同的代码,以显示您要查找的正确日期和时间格式。干杯

http://www.alexcurylo.com/blog/2009/01/29/nsdateformatter-formatting/

我正在创建一个应用程序,该应用程序使用UIDatePicker为事件选择日期和时间(故事板中有一个选项)。

我想知道如何在选择/保存后在标签中显示日期和时间。现在,这就是我在整个应用程序中的部分代码。我的问题是:我是否更改格式化程序代码以允许UIDatePicker在我的应用程序中显示日期和时间?有办法做到这一点吗?提前感谢!

MasterViewController

    -(void)configureCell:(MasterEventCell *)cell atIndexPath:(NSIndexPath *)indexPath{
    static NSDateFormatter *formatter = nil;
    if (formatter == nil) {
        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
    }
    Event *eventAtIndex = [_fetchedResultsController objectAtIndexPath:indexPath];
    cell.eventDetailsLabel.text = eventAtIndex.detail;
    cell.eventLocationLabel.text = eventAtIndex.location;
    cell.eventDateTimeLabel.text = [formatter stringFromDate:(NSDate *)eventAtIndex.date];
}

DetailViewController

    - (void)configureView
{
    Event *theEvent = self.event;
    static NSDateFormatter *formatter = nil;
    if (formatter == nil) {
        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
    }
    if (theEvent) {
        self.detailLabel.text = theEvent.detail;
        self.locationLabel.text = theEvent.location;
        self.dateTimeLabel.text = [formatter stringFromDate:(NSDate *)theEvent.date];
    }
}

在日期格式化程序上也调用setTimeStyle:

最新更新