我在这里要做的是使用 NavigationLink 将“KarvonenVal”的值移动到 SummaryView。
struct CalcProcess: View{
@EnvironmentObject var workoutManager: WorkoutManager
@State var NumAdded4 = false
@State var Age:Int
@State var ExerciseIT:Int
@State var ConstantNumber = 220
@State var RHR:Int
@State var KarvonenVal = 1
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(.black)
}
NavigationLink(destination: SummaryView(NumAdded4: $NumAdded4, KarvonenVal: KarvonenVal), isActive: $NumAdded4, label: {Text("Next")})
}
}
}
struct KarvonenCalc_Previews: PreviewProvider {
static var previews: some View {
KarvonenCalc(KarvonenVal: Int)
}
}
但是,我在“KarvonenCalc_Previews”中不断收到错误消息,上面写着“无法将 'Int.Type' 类型的值转换为预期的参数类型 'Int'”。我真的被困在这里,无法在 SummaryView 中显示 KarvonenVal。
struct KarvonenCalc_Previews: PreviewProvider {
static var previews: some View {
KarvonenCalc(KarvonenVal: Int)
}
}
同样在 SummaryView_Preview,我收到了类似的错误,上面写着“无法将 'String.Type' 类型的值转换为预期的参数类型 'String'”。
struct SummaryView_Previews: PreviewProvider {
static var previews: some View {
SummaryView(NumAdded4: .constant(false), KarvonenVal: String)
}
}