我是IOS和SwiftUI编码的新手。我用谷歌搜索了很多,但是找不到解决方案,如何将计算变量传递给下一个视图。 以下是我所拥有的片段:
import SwiftUI
struct ContentView: View {
@State private var isShowingResultView = false
@State private var netRate = "0"
@State var daysMonth = "0"
@State var hoursWeek: String = "0"
@State var daysOnsite: String = "0"
@State var ticketCost: String = "0"
@State var hotelCost: String = "0"
@State var otherCost: String = "0"
//@State var travellCostResult: Double = 0.00
var travellCostPerHour: Double{
get {
let daysMonthNbr = Int(daysMonth) ?? 0
let hoursWeekNbr = Int(hoursWeek) ?? 0
let daysOnsiteNbr = Int(daysOnsite) ?? 0
let ticketCostNbr = Double(ticketCost) ?? 0
let hotelCostNbr = Double(hotelCost) ?? 0
let otherCostNbr = Double(otherCost) ?? 0
let travellCostPerWeek = (ticketCostNbr + (Double((daysOnsiteNbr-1))*hotelCostNbr)+otherCostNbr)
let travellCostPerHour: Double = Double(travellCostPerWeek) / Double(hoursWeekNbr)
return travellCostPerHour.isNaN ? 0 : travellCostPerHour
}
}
.
.
.
var body: some View {
HStack {
NavigationLink("Calculate", destination: ResultView(netRate: self.$netRate, travellCostPerHour: travellCostPerHour), isActive: $isShowingResultView).navigationBarTitle("Result").buttonStyle(GradientButtonStyle())
.adaptToKeyboard()
}
struct ResultView: View {
@Binding var netRate: String
@Binding var travellCostPerHour: Double
.
.
.
struct ResultView_Previews: PreviewProvider {
@State static var netRate: String = ""
@State static var travellCostPerHour: Double = 0.00
static var previews: some View {
ResultView(netRate: $netRate, travellCostPerHour: $travellCostPerHour )
}
}
我在travellCostPerHour的navigationLink中收到此错误消息:无法将类型“ Double”的值转换为预期的参数类型“ Binding” 一个人可以把我放在正确的道路上吗?
答案 0 :(得分:0)
如果是计算属性,则不需要绑定,请按原样传递
struct ResultView: View {
@Binding var netRate: String
var travellCostPerHour: Double // << just regular
// .. other code