【问题标题】:How do I set a datePicker's display to an AppStorage time (hour, min) settings?如何将 datePicker 的显示设置为 AppStorage 时间(小时、分钟)设置?
【发布时间】:2021-06-30 02:21:24
【问题描述】:

我试图让用户通过 datePicker 上的时间选择来选择他们希望通知在什么时间发出。我将 datePicker 设置为仅显示时间,如下所示:

DatePicker("First Workout Notification Time", selection: $firstNotificationTime, displayedComponents: .hourAndMinute)

我希望 firstNotificationTime @State var 使用我的 @AppStorage 时间变量,这些时间变量在此页面上的“var body: some View”上方设置为默认值:

@AppStorage("startNoficationHour") var startNoficationHour: Int = 9
@AppStorage("startNoficationMin") var startNoficationMin: Int = 30

当视图加载时(即 .onAppear(){})我希望将 datePicker 的 $firstNotificationTime 设置为 startNoficationHour 和 startNoficationMin

我的视图设置如下:

var body: some View {
        VStack {
DatePicker("First Workout Notification Time", selection: $firstNotificationTime, displayedComponents: .hourAndMinute)
}
}
.onAppear(){
// set datePicker's fisrtNotificationTime to startNotificationHour and startNotificationMin
let components = DateComponents(calendar: Calendar.current, timeZone: TimeZone.current, hour:startNoficationHour, minute: startNoficationMin)
firstNotificationTime = components.date!        
                        print("start hour is \(startNoficationHour) and start minute is \(startNoficationMin)")
}
.onDisappear(){
// sets startNotificationHour and startNotificationMin to datePicker
                        let components = Calendar.current.dateComponents([.hour, .minute], from: firstNotificationTime)
                        startNoficationHour = components.hour!
                        startNoficationMin = components.minute!
                        print("start hour is set to \(startNoficationHour) and start minute is set to \(startNoficationMin)")
}

【问题讨论】:

  • 对,但我不想添加到当前日期,我只想将其设置为保存的时间。我已经从该示例中获取了代码并在上面编辑了我的问题。因为它正在添加到当前日期,所以它不能正常工作。我想我对 DateComponents 了解得不够透彻。

标签: ios iphone xcode ipad swiftui


【解决方案1】:

好的,所以我要做的就是将组件变量设置为小时和分钟,并将其放在 onAppear 中

let components = DateComponents(calendar: Calendar.current, timeZone: TimeZone.current, hour:startNoficationHour, minute: startNoficationMin)
firstNotificationTime = components.date!

我根据上面 Tushar Sharma 与Set default date on DatePicker in SwiftUI? 相关的评论更新了我上面的问题的答案

【讨论】:

    【解决方案2】:

    你做了很多事情。有简单的解决方案。使用阴影属性。

    Date 具有将时间转换为双精度值的方法。因此,您可以将 double 值存储到 @AppStorage 并使用它来更新日期。

    这是一个例子,

    创建一个@State 日期变量,

    @State var reminderTime = Date()

    创建一个显示属性保存日期。

    @AppStorage("reminderTime") var reminderTimeShadow: Double = 0

    现在在DatePicker

     DatePicker("", selection: Binding(
                                get: {reminderTime}, set: {
                                    reminderTimeShadow = $0.timeIntervalSince1970
                                    reminderTime = $0
                                    setNotification()
                                }), displayedComponents: .hourAndMinute)   
                        .onAppear {
                            reminderTime = Date(timeIntervalSince1970: reminderTimeShadow)
                        }
    

    在这里,您使用显式绑定来获取和设置值,在设置提醒时间的值时,您还设置了值reminderTimeShadow 并将其保存为@AppStorage,并在.onAppear 中使用@987654331 @ 从而更新reminderTime 和视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 2022-01-25
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多