Swift UI DatePicker不会更新@ObservedObject值

时间:2020-08-09 19:03:17

标签: swift datepicker swiftui combine observedobject

我正在使用一个类来存储@Published变量,但是当我尝试将在自定义DatePicker上选择的值传递给该类中的@ObservedObject时,会出现以下错误:

TimePicker(time: self.$time.**timeSelected**)

无法转换类型为“绑定”(aka的值) “绑定”)到预期的参数类型“ TimeModel”

如何使用选择器值更新@ObservedObject?

完整代码:

struct ContentView: View {
    
    @ObservedObject var time = TimeModel()

    var body: some View {

        ZStack{
            VStack{
                TimePicker(time: self.$time.timeSelected)
                
                Text("You chose \(time.timeSelected/60) minutes")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

class TimeModel: ObservableObject{
     @Published var timeSelected: TimeInterval = 1.0
}
    

struct TimePicker: UIViewRepresentable {
    
    @ObservedObject var time = TimeModel()
    
    func makeUIView(context: Context) -> UIDatePicker {
        let datePicker = UIDatePicker()
        datePicker.datePickerMode = .countDownTimer
        datePicker.addTarget(context.coordinator,
                             action: #selector(Coordinator.updateTime),
                             for: .valueChanged)
        return datePicker
    }
    
    func updateUIView(_ datePicker: UIDatePicker, context: Context) {
        datePicker.minuteInterval = 5
        datePicker.countDownDuration = time.timeSelected
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject {
        let parent: TimePicker
        
        init(_ parent: TimePicker) {
            self.parent = parent
        }
        
        @objc func updateTime(datePicker: UIDatePicker) {
            parent.time.timeSelected = datePicker.countDownDuration
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要引用 same TimeModel实例。

一种解决方案是将TimeModel传递到TimePicker

struct TimePicker: UIViewRepresentable {
    @ObservedObject var time: TimeModel // <- declare only

    ...
}

struct ContentView: View {
    @ObservedObject var time = TimeModel()

    var body: some View {
        ZStack {
            VStack {
                TimePicker(time: time) // <- pass `TimeModel` here

                Text("You chose \(time.timeSelected / 60) minutes")
            }
        }
    }
}

请注意,TimeInterval指定的时间以秒为单位,而不是以分钟为单位。

此行:

@Published var timeSelected: TimeInterval = 1.0

实际上将默认时间设置为1秒。