UIDatePicker 反映选择的时间

UIDatePicker reflect chosen time

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

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

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: { [=11=].identifier == "notInt" }) else { return }
    guard let trigger = notification.trigger as? UNCalendarNotificationTrigger else { return }
    notificationTime.date = trigger.nextTriggerDate()
}

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