Swiftui中新Textfields的不同状态变量

发布于 2025-01-23 20:24:21 字数 1845 浏览 0 评论 0原文

我是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.

The scenario is like this:
scenario

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2025-01-30 20:24:21

您想处理多个人,但只有一个 name一个 电子邮件属性。

您需要一个数组。 中的自定义结构

struct Person  {
    var name, email : String
}

替换name电子邮件 person

@State private var people = [Person]()

a swifty 方式是视图 foreach循环在索引上迭代,并以给定索引将文本参数绑定到该人。

我没有您的自定义文本字段,该代码

ForEach(people.indices, id: \.self) { index in
    VStack {
        TextField("Enter name", text: $people[index].name)
        TextField("Enter email", text: $people[index].email)
    }
    .padding()
}

最终在按钮操作中使用默认字段添加了一个新的Person People

Button {
    print("add person tapped")
    people.append(Person(name: "", email: ""))

You want to handle multiple people but you have only one name and one email property.

You need an array. A swifty way is a custom struct

struct Person  {
    var name, email : String
}

In the view replace name and email with an empty array of Person

@State private var people = [Person]()

In the ForEach loop iterate over the indices and bind the text parameter to the person at given index.

I don't have your custom text fields, the code uses the default fields

ForEach(people.indices, id: \.self) { index in
    VStack {
        TextField("Enter name", text: $people[index].name)
        TextField("Enter email", text: $people[index].email)
    }
    .padding()
}

Finally in the button action add a new Person to people

Button {
    print("add person tapped")
    people.append(Person(name: "", email: ""))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文