Swift UI DatePicker 不会更新@ObservedObject 值
Swift UI DatePicker won't update @ObservedObject value
我正在使用 class 来存储 @Published 变量,但是当我尝试将在我的自定义 DatePicker 上选择的值传递给 class 中的 @ObservedObject 时,我收到以下错误:
TimePicker(time: self.$time.**timeSelected**)
Cannot convert value of type 'Binding' (aka
'Binding') to expected argument type '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
}
}
}
您需要参考相同 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 秒。
我正在使用 class 来存储 @Published 变量,但是当我尝试将在我的自定义 DatePicker 上选择的值传递给 class 中的 @ObservedObject 时,我收到以下错误:
TimePicker(time: self.$time.**timeSelected**)
Cannot convert value of type 'Binding' (aka 'Binding') to expected argument type '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
}
}
}
您需要参考相同 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 秒。