UIDatePicker反映了选定的时间

问题描述 投票:0回答:1

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

ios swift xcode uidatepicker
1个回答
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。

© www.soinside.com 2019 - 2024. All rights reserved.