如何分隔每个文本字段的输入值?
How to separate input values for each Textfield?
我将用户的输入存储到字典中,但变量名称和数量似乎不是每个文本字段行的单独值。我尝试添加自我。名称和数量,但这似乎无济于事。我该如何实施?
@Binding var numPeople: Int
@State var dict: [String : Float] = [:]
@State var name: String = ""
@State var amount: Float = 0.00
.
.
.
ForEach(1...numPeople, id:\.self) { stack in
HStack {
TextField("Name", text: $name)
.padding()
Text("Amount in $:")
TextField("", value: $amount, formatter: NumberFormatter())
.keyboardType(.numberPad)
.onReceive(Just(amount)) { _ in
dict[name] = amount
}
.padding()
}
}
谢谢!
在您的代码中,您对使用 ForEach
迭代的所有行使用了相同的变量 name
和 amount
。如果你想让每一行都有自己的字段单独管理,你需要分开视图。
下面是一个非常示意性的例子,说明它是如何工作的:
在父视图中,ForEach
会调用一个子视图:
@Binding var numPeople: Int
// Make @State vars private
@State private var dict: [String : Float] = [:]
// Note that you don't use the variables name and amount here
.
.
.
ForEach(1...numPeople, id:\.self) { stack in
// Pass the dictionary, it will be updated by the subview
SubView(dict: $dict)
}
创建一个将单独管理每个名称/金额的子视图:
struct SubView: View {
@Binding var dict: [String : Float]
@State private var name: String = ""
@State private var amount: Float = 0.00
var body: some View {
HStack {
TextField("Name", text: $name)
.padding()
Text("Amount in $:")
TextField("", value: $amount, formatter: NumberFormatter())
.keyboardType(.numberPad)
// I don't know why you need this, if the amount is
// updated in this view. Maybe you can just use
// dict[name] = amount, dropping the .onReceive()...
// ... but it depends on your code
.onReceive(Just(amount)) { _ in
dict[name] = amount
}
.padding()
}
}
private func whatToDoWithNameAndAmount() {
// Do whatever else you need with these variables
}
}
我将用户的输入存储到字典中,但变量名称和数量似乎不是每个文本字段行的单独值。我尝试添加自我。名称和数量,但这似乎无济于事。我该如何实施?
@Binding var numPeople: Int
@State var dict: [String : Float] = [:]
@State var name: String = ""
@State var amount: Float = 0.00
.
.
.
ForEach(1...numPeople, id:\.self) { stack in
HStack {
TextField("Name", text: $name)
.padding()
Text("Amount in $:")
TextField("", value: $amount, formatter: NumberFormatter())
.keyboardType(.numberPad)
.onReceive(Just(amount)) { _ in
dict[name] = amount
}
.padding()
}
}
谢谢!
在您的代码中,您对使用 ForEach
迭代的所有行使用了相同的变量 name
和 amount
。如果你想让每一行都有自己的字段单独管理,你需要分开视图。
下面是一个非常示意性的例子,说明它是如何工作的:
在父视图中,ForEach
会调用一个子视图:
@Binding var numPeople: Int
// Make @State vars private
@State private var dict: [String : Float] = [:]
// Note that you don't use the variables name and amount here
.
.
.
ForEach(1...numPeople, id:\.self) { stack in
// Pass the dictionary, it will be updated by the subview
SubView(dict: $dict)
}
创建一个将单独管理每个名称/金额的子视图:
struct SubView: View {
@Binding var dict: [String : Float]
@State private var name: String = ""
@State private var amount: Float = 0.00
var body: some View {
HStack {
TextField("Name", text: $name)
.padding()
Text("Amount in $:")
TextField("", value: $amount, formatter: NumberFormatter())
.keyboardType(.numberPad)
// I don't know why you need this, if the amount is
// updated in this view. Maybe you can just use
// dict[name] = amount, dropping the .onReceive()...
// ... but it depends on your code
.onReceive(Just(amount)) { _ in
dict[name] = amount
}
.padding()
}
}
private func whatToDoWithNameAndAmount() {
// Do whatever else you need with these variables
}
}