我确定我做错了,但我基本上想创建类似于 Apple 闹钟的东西。所以我有一个只显示小时和分钟的 UIDatePicker。我的计划是从 UIDatePicker 中获取小时和分钟,然后为当前日期或下一个日期的该小时和分钟创建一个 UILocalNotification,具体取决于当天的警报是否可用。所以当我的视图加载时我尝试做的是:
[self.alarmsDatePicker addTarget:self action:@selector(datePickerDidSelect:) forControlEvents:UIControlEventValueChanged];
[self.alarmsDatePicker setDate:[NSDate date]];
// Get the current calendar
self.calendar = [NSCalendar currentCalendar];
self.dateFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
// Get the time portion of the datePicker's date
NSDateComponents *timeComponentsSelectedDate = [_calendar components:_dateFlags fromDate:self.alarmsDatePicker.date];
NSDate *timeOnlySelectedDate = [_calendar dateFromComponents:timeComponentsSelectedDate];
self.startDate = timeOnlySelectedDate;
当我单步执行代码时,我不明白的是,datePicker 获取正确启动的当前时间。但是当我到达 timeOnlySelectedDate 时,日期是 2001-01-01。这是为什么?提前致谢。