我在这里要做的是将“KarvonenVal”传递给另一个 SwiftUI 文件中的不同视图。我尝试过使用 .environmentObject 和 observableObject,但它们都不起作用。我的目标是在 SummaryView 上显示 KarvonenVal。感谢您的帮助。
值来自
struct CalcProcess: View{
@Binding var NumAdded3:Bool
@State var Age:Int
@State var ExerciseIT:Int
@State var ConstantNumber = 220
@State var RHR:Int
func karvonen(cn: Int, rhr: Int, age: Int, ei:Double) -> Double {
return Double((cn-age-rhr)) * (ei / 10) + Double(rhr)
}
var body: some View {
let output = karvonen(cn: ConstantNumber, rhr: RHR, age: Age, ei: Double(ExerciseIT))
let roundedDouble = Double(round(1000*output)/1000)
let KarvonenVal: String = String(format: "%.1f", roundedDouble)
VStack{
Text("\(KarvonenVal)")
.foregroundStyle(.red)
}
}
}
到这里
struct SummaryView: View {
@EnvironmentObject var workoutManager: WorkoutManager
@Environment(\.dismiss) var dismiss
var body: some View {
ScrollView {
VStack(alignment: .leading) {
SummaryMetricView(title: "Total Time",
value: durationFormatter.string(from: workoutManager.workout?.duration ?? 0.0) ?? "")
.foregroundStyle(.yellow)
SummaryMetricView(title: "Avg. Heart Rate",
value: workoutManager.averageHeartRate.formatted(.number.precision(.fractionLength(0))) + " bpm")
.foregroundStyle(.red)
SummaryMetricView(title: "Target Heart Rate",
value: KarvonenVal + " bpm")
.foregroundStyle(.red)
Button("Done") {
dismiss()
}
}
.scenePadding()
}
.navigationTitle("Summary")
.navigationBarTitleDisplayMode(.inline)
}
}