簡體   English   中英

Swift 界面 | 文本字段未讀取輸入的值

[英]Swift UI | Textfield not reading entered value

我有一個文本字段,它應該記錄某人吃過的食品的單位,然后用於計算用戶消耗的卡路里、蛋白質等的總數。 但是當在文本字段中輸入值時, units變量不會更新。 我怎樣才能解決這個問題?

這是我的代碼:

@State var selectFood = 0
@State var units = 0
@State var quantity = 1.0
    
@State var caloriesInput = 0.0
@State var proteinInput = 0.0
@State var carbsInput = 0.0
@State var fatsInput = 0.0

var body: some View {
    VStack {
        
        Group {
            Picker(selection: $selectFood, label: Text("What did you eat?")
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(.white))
            {
                ForEach(database.productList.indices, id: \.self) { i in
                    Text(database.productList[i].name)
                }
            }
            .pickerStyle(MenuPickerStyle())
                
            Spacer(minLength: 25)
                
            Text("How much did you have?")
                .font(.headline)
                .fontWeight(.bold)
                .foregroundColor(.white)
                .frame(alignment: .leading)

            //Textfield not working.
            TextField("Units", value: $units, formatter: NumberFormatter())
                .padding(10)
                .background(Color("Settings"))
                .cornerRadius(10)
                .foregroundColor(Color("Background"))
                .keyboardType(.numberPad)
                
            Button (action: {
                self.quantity = ((database.productList[selectFood].weight) * Double(self.units)) / 100
                    
                caloriesInput = database.productList[selectFood].calories * quantity
                proteinInput = database.productList[selectFood].protein * quantity
                carbsInput = database.productList[selectFood].carbs * quantity
                fatsInput = database.productList[selectFood].fats * quantity
                    
                UIApplication.shared.hideKeyboard()
                    
            }) {
                ZStack {
                    Rectangle()
                        .frame(width: 90, height: 40, alignment: .center)
                        .background(Color(.black))
                        .opacity(0.20)
                        .cornerRadius(15)
                                    ;
                    Text("Enter")
                        .foregroundColor(.white)
                        .fontWeight(.bold)
                } 
            } 
        }
    }
}

這是NumberFormatter的一個問題,已經持續了一段時間。 如果您刪除格式化程序,它會正確更新。

這是一種解決方法。 遺憾的是它需要 2 個變量。

import SwiftUI

struct TFConnection: View {
    @State var unitsD: Double = 0
    @State var unitsS = ""
    
    var body: some View {
        VStack{
            //value does not get extracted properly
            TextField("units", text: Binding<String>(
                        get: { unitsS },
                        set: {
                            if let value = NumberFormatter().number(from: $0) {
                                print("valid value")
                                self.unitsD = value.doubleValue
                            }else{
                                unitsS = $0
                                //Remove the invalid character it is not flawless the user can move to in-between the string
                                unitsS.removeLast()
                                print(unitsS)
                            }
                        }))
            
            Button("enter"){
                print("enter action")
                print(unitsD.description)
            }
        }
    }
}

struct TFConnection_Previews: PreviewProvider {
    static var previews: some View {
        TFConnection()
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM