我目前正在学习combine 和MVVM。我的问题是,当我尝试使用timer.publish时,最终我将创建一个停止按钮,它会导致整个屏幕刷新而不是我的.onReceive文本。
我希望有人能给我一些关于我如何错误地使用发布者和观察者的见解。
看法:
import SwiftUI
import Combine
struct ApptCardView: View {
@ObservedObject var apptCardVM: ApptCardViewModel
@State var currentDate = Date()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Text("\(currentDate)")
.onReceive(timer) { input in
self.currentDate = input
}
Picker("Seizure Type", selection: $apptCardVM.typeIndex) {
ForEach(0..<apptCardVM.typeChoice.count) {
Text(self.apptCardVM.typeChoice[$0])
}
}.pickerStyle(SegmentedPickerStyle())
}
}
}
Run Code Online (Sandbox Code Playgroud)
查看型号:
import Foundation
import Combine
class ApptCardViewModel: ObservableObject, Identifiable {
@Published var appt: ApptEvent
@Published var typeChoice = ["Quick", "Long", …Run Code Online (Sandbox Code Playgroud)