SwiftUI 选择器 VALUE

SwiftUI Picker VALUE

我最近一直在尝试用 swiftui 编程一些东西, 很难,我怎样才能将选择器的值转移到 文本字段,我很绝望!

为什么我不能像在文本字段中那样直接使用值 $khValue?

我已经花了几个小时在互联网上搜索……我还没有找到任何东西,swiftUI 与 swift

完全不同
import SwiftUI

struct KH_aus_Co2: View {
    @State private var kh_Picker : String = ""
    @State private var ph_Picker: String = ""
    
    
    
    var kh_vol = [Int](0..<21)
    var ph_vol = [Int](0..<10)
    
    init(){
        UITableView.appearance().backgroundColor = .clear
    }
    
    @State private var khWert: String = ""
    @State private var phWert: String = ""
    @State private var phco2Wert: String = ""
    
    
    
    var calculation: String {
        
        guard khWert.isEmpty == false, phWert.isEmpty == false else { return "" }
        
        
        guard let kh = Double(khWert), let ph  = Double(phWert) else { return "Error" }
        
        let product = kh/2.8 * pow(10,7.90-ph)
        
        
        return String(format: "%.2f", product)
    }
    
    
    
    var body: some View {
        
        VStack() {
            Text("Co2 = \(calculation) mg/ltr")
                
                .font(.largeTitle)
                .multilineTextAlignment(.center)
                .foregroundColor(Color.green)
                .frame(width: 300, height: 60, alignment: .center)
                .border(Color.green)
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
            
            
            
            HStack {
                
                
                TextField("KH Wert", text: $khWert)
                    .border(Color.green)
                    .frame(width: 120, height: 70, alignment: .center)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    //                            .textContentType(.oneTimeCode)
                    .keyboardType(.numberPad)
                
                TextField("PH Wert", text: $phWert)
                    .border(Color.green)
                    .frame(width: 120, height: 70, alignment: .center)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    //                            .textContentType(.oneTimeCode)
                    .keyboardType(.numberPad)
                
            }
            
            GeometryReader { geometry in
                HStack {
                    
                    Picker(selection: self.$kh_Picker, label: Text("")) {
                        ForEach(0 ..< self.kh_vol.count) { index in
                            Text("\(self.kh_vol[index])").tag(index)
                            //
                        }
                        
                    }
                    .frame(width: geometry.size.width/3, height: 100, alignment: .center) .clipped()
                    
                    Picker(selection: self.$ph_Picker, label: Text("")) {
                        ForEach(0 ..< self.ph_vol.count) { index in
                            Text("\(self.ph_vol[index])").tag(index)
                        }
                        
                    }
                    .frame(width: geometry.size.width/3, height: 100, alignment: .center) .clipped()
                    
                    
                }
            }
            
        }
            
            
        .navigationBarTitle(Text("Co2 aus KH & PH"))
        .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
        
        
    }
    
    
    
}

struct KH_aus_Co2_Previews: PreviewProvider {
    static var previews: some View {
        Co2_aus_KH()
        
    }
}

非常感谢您的帮助

于尔根......................................

如果我的理解正确,您希望选择器更新文本字段中的值。如果是这样,那么您希望将相同的值绑定到文本字段正在使用的选择器。由于该值是 String,您需要在选择器中使用 String 值。

使用.map(String.init)kh_volph_vol范围变成String的数组,并使用\.self作为id:

Picker(selection: self.$khWert, label: Text("")) {
    ForEach(self.kh_vol.map(String.init), id: \.self) { index in
        Text(index)
    }
    
}
.frame(width: geometry.size.width/3, height: 100, alignment: .center)
.clipped()

Picker(selection: self.$phWert, label: Text("")) {
    ForEach(self.ph_vol.map(String.init), id: \.self) { index in
        Text(index)
    }
    
}
.frame(width: geometry.size.width/3, height: 100, alignment: .center)
.clipped()