2

I've been trying to program something with swiftui recently, it's difficult, how can I transfer the value of the picker into the text field, I'm desperate!

and why can I not work with the value $ khValue directly as in the text field?

I've already spent hours searching the internet… I haven't found anything yet, swiftUI is completely different from 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() } } 

big thanks for your help

Jürgen....................................

1
  • First of all Picker selection and tag (or id) should be the same type, so selection work (your selection is String, but tag is Int, so it wan't work). But in general your post is not clear. What do you want to achieve? What's the goal? Would you clarify (maybe minimise example to a problem area). Commented Sep 1, 2020 at 16:54

1 Answer 1

1

If I understand you correctly, you want the pickers to update the values in the textfields. If so, then you want to bind the same value to the picker that the textfield is using. Since that value is a String, you will want to use String values in your picker.

Use .map(String.init) to turn the kh_vol and ph_vol ranges into arrays of String, and use \.self as the 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() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.