UIDatePicker反映了选定的时间

时间:2018-01-27 13:31:56

标签: ios swift xcode uidatepicker

我正在使用UIDatePicker来允许用户在我的应用中为每日通知选择时间。但是,它不会将所选时间的值保留在实际选取器上。如何让UIDatePicker反映他们选择的时间? Setup of UIDatePicker and Notifications page

1 个答案:

答案 0 :(得分:0)

我不知道您如何存储通知时间,但您有责任将日期选择器设置为该时间。例如,我在这里设置一个选择器来显示七天前的时间:

let oneWeekAgo = Calendar.current.date(byAdding: .day, value: -7, to: Date())!
datePicker.setDate(oneWeekAgo, animated: true)

您的问题是因为您没有将UIDatePicker时间保存在除通知本身以外的某个地方时,您不确定在编辑它时如何将其设置回UIDatePicker。你可以尝试这样的事情:

UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
    guard let notification = requests.first(where: { $0.identifier == "notInt" }) else { return }
    guard let trigger = notification.trigger as? UNCalendarNotificationTrigger else { return }
    notificationTime.date = trigger.nextTriggerDate()
}

...获取所有待处理的通知请求,找到与您设置的标识符匹配的请求,并从中获取时间。上面的notificationTime是UIDatePicker。