【问题标题】:Swift UI | Textfield not reading entered value斯威夫特用户界面 |文本字段未读取输入的值
【发布时间】:2020-10-25 20:18:28
【问题描述】:

我有一个文本字段,它应该记录某人吃过的食品的单位,然后用于计算用户消耗的卡路里、蛋白质等的总数。但是当在文本字段中输入值时,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)
                } 
            } 
        }
    }
}

【问题讨论】:

  • units 是如何声明的?
  • @JoakimDanielson 我已经添加了初始化变量
  • 带有格式化程序的文本字段仅在提交时更新绑定变量。以下内容应该会有所帮助stackoverflow.com/a/59509242/12299030

标签: ios swift xcode swiftui uitextfield


【解决方案1】:

这是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()
    }
}

【讨论】:

    猜你喜欢
    • 2023-02-23
    • 2017-07-22
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多