我有这个带有SwiftUI的程序。该程序用于基于3个用户输入的机器学习来计算就寝时间。我有一个Text(“”)向用户显示他们更新的就寝时间。
我希望程序自动更新就寝时间并将其显示在我的Text(“”)上。我尝试了很多方法,但似乎都没有用。到目前为止我尝试过的
有人知道是否有一种更简便的方法来更新就寝时间,但是用户滚动选择器并在变量发生变化时进行滚动?
struct ContentView: View {
static var defaultWakeUpTime : Date {
var defaultTime = DateComponents()
defaultTime.hour = 7
defaultTime.minute = 0
return Calendar.current.date(from: defaultTime) ?? Date()
}
@State private var wakeUp = defaultWakeUpTime
@State private var sleepAmount = 8.0
@State private var coffeeAmount = 0 {
didSet {
calculateSleepTime()
}
}
@State private var showTime : String = " "
func calculateSleepTime() {**CONTENT**}
var body: some View {
NavigationView {
VStack {
Spacer(minLength: 20)
Text("Your optimum sleep time is \(showTime)")
Spacer(minLength: 10)
Section {
Text("When do you want to wake up?")
.font(.headline)
DatePicker("Please choose a time", selection: $wakeUp, displayedComponents: .hourAndMinute)
.labelsHidden()
.datePickerStyle(WheelDatePickerStyle())
}
Spacer()
Form {
Text("How many hours would you like to sleep?")
.font(.headline)
Stepper(value: $sleepAmount, in: 4...12, step: 0.25) {
Text("\(sleepAmount, specifier: "%g" ) hours")
}
}
Spacer()
Section {
Text("How many cups of coffee do you drink?")
.font(.headline)
Picker("Coffee Selector", selection: $coffeeAmount) {
ForEach (1..<21) {
Text("\($0) " + "Cup")
}
}
.labelsHidden()
}
}
.navigationBarTitle(Text("BetterSleep"))
.onAppear(perform: calculateSleepTime)
}
}
}
答案 0 :(得分:0)
我将使用viewModel并使用订阅来跟踪值并计算睡眠时间。
将顶部的ContentView更改为此
struct ContentView: View {
@ObservedObject var viewModel = ViewModel()
现在在所有变量之前加上viewModel.
创建一个新的.swift文件,我只是将其命名为ViewModel,但是您不必这样做。
import Combine
final class ViewModel: ObservableObject {
@Published private(set) var bedTime: String = ""
@Published var wakeUp: Date = Date()
@Published var sleepAmount: Double = 8.0
@Published var coffeeAmount = 0
private var cancellables = Set<AnyCancellable>()
init() {
$wakeUp
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.calculateSleepTime()
}.store(in: &cancellables)
$sleepAmount
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.calculateSleepTime()
}.store(in: &cancellables)
$coffeeAmount
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.calculateSleepTime()
}.store(in: &cancellables)
}
private func calculateSleepTime() {
// Your Logic
self.bedTime =
}
}
现在,只要其中一个值发生更改,建议的就寝时间就会更新。请记住,从0开始,将其添加到coffeeAmount中。