我有这个带有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)
}
}
}
我有这个带有SwiftUI的程序。该程序用于基于3个用户输入的机器学习来计算就寝时间。我有一个Text(“”)向用户显示他们更新的就寝时间。我想要该程序...
我将使用viewModel并使用订阅来跟踪值并计算睡眠时间。