Swiftui中新Textfields的不同状态变量
我是Swiftui的新手,在某种情况下,我可以在 button上添加一个以上的数据,每次iem> tap button 时,它都会收集新人的数据。
我在一个Textfield上添加了数据,它在每个TextField上更新,因为Textfield只有一个状态变量。我的问题是如何为Textfield添加多个状态变量,因为Textfields没有固定的数字。我的代码是:
import SwiftUI
struct TextFieldText: View {
@State private var name = ""
@State private var email = ""
@State var totalValue: Int = 1
var body: some View {
VStack(spacing: 30) {
ForEach((1...totalValue).reversed(), id: \.self) {_ in
VStack {
CustomTextfield(text: $name, placeHolder: "Enter name", title: "Enter Name")
CustomTextfield(text: $email, placeHolder: "Enter email", title: "Enter Email")
}
}
Button {
print("add person tapped")
totalValue = totalValue + 1
} label: {
ZStack {
RoundedRectangle(cornerRadius: 60)
.frame(width: 180, height: 45)
.foregroundColor(Color(ColorName.appBlue.rawValue))
Text("Add another person")
.foregroundColor(.white)
.font(Font.custom(InterFont.bold.rawValue, size: 14))
}
}
Spacer()
}
}
}
struct TextFieldText_Previews: PreviewProvider {
static var previews: some View {
TextFieldText()
}
}
我想在每个Textfield上添加不同的数据。我该如何在Swiftui中实现?
I am new to SwiftUI and there is a scenario in which I can add more than one person's data and every time I tap on the button, it will collect new person's data.
I add data on one textfield, it updates on every textfield because there is only one state variable for the textfield. My problem is how can I add multiple State variables for the textfield as the textfields have no fixed number. My code is:
import SwiftUI
struct TextFieldText: View {
@State private var name = ""
@State private var email = ""
@State var totalValue: Int = 1
var body: some View {
VStack(spacing: 30) {
ForEach((1...totalValue).reversed(), id: \.self) {_ in
VStack {
CustomTextfield(text: $name, placeHolder: "Enter name", title: "Enter Name")
CustomTextfield(text: $email, placeHolder: "Enter email", title: "Enter Email")
}
}
Button {
print("add person tapped")
totalValue = totalValue + 1
} label: {
ZStack {
RoundedRectangle(cornerRadius: 60)
.frame(width: 180, height: 45)
.foregroundColor(Color(ColorName.appBlue.rawValue))
Text("Add another person")
.foregroundColor(.white)
.font(Font.custom(InterFont.bold.rawValue, size: 14))
}
}
Spacer()
}
}
}
struct TextFieldText_Previews: PreviewProvider {
static var previews: some View {
TextFieldText()
}
}
I want to add different data on every textfield. How can I achieve it in SwiftUI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想处理多个人,但只有一个
name
和一个电子邮件
属性。您需要一个数组。 中的自定义结构
替换
name
和电子邮件
person
a swifty 方式是视图
foreach
循环在索引上迭代,并以给定索引将文本
参数绑定到该人。我没有您的自定义文本字段,该代码
最终在按钮操作中使用默认字段添加了一个新的
Person
People
You want to handle multiple people but you have only one
name
and oneemail
property.You need an array. A swifty way is a custom struct
In the view replace
name
andemail
with an empty array ofPerson
In the
ForEach
loop iterate over the indices and bind thetext
parameter to the person at given index.I don't have your custom text fields, the code uses the default fields
Finally in the button action add a new
Person
topeople