Action Button(Textfield)

Good day, I would like to generate a text field when I tap on the green button that creates a new text field and I can still enter for example a 2nd phone number. I have already tried it with this example:

Button(action: { print("Telefonnummer")}) {
          Image(systemName: "plus.circle.fill")
                .foregroundColor(Color(.systemGreen)) 
               }
  • Can you clarify what you want to ask?

  • So if you tap on the plus button in the text field, I want it to create a new text field if you have several phone numbers, for example.

  • What I do not understand is that your code shown does not contain any TextField. You should better show more code even if it might not be perfect. That would explain what you want to do better than the too simple example.

Replies

struct heartTextSquareView: View {
    @State private var name: String = ""
    @State private var adresse: String = ""
    @State var telefonnummer: String = ""
    @State var something: String = ""
    var body: some View {
        VStack {
            List {
                TextField("Name/Vorname", text: $name)
                TextField("Adresse", text: $adresse)
                
                Button(action: { print("Telefonnummer")}) {
                               Image(systemName: "plus.circle.fill")
                                    .foregroundColor(Color(.systemGreen))

                }                

            }
  • So that's a larger part of my code, I hope that helps.

  • Thanks for trying to show more code. If you could show a code which can build that would be better.

  • One more, type names should start with Capital letters in Swift.

Add a Comment

Please try something like this:

import SwiftUI

struct HeartTextSquareView: View {
    @State private var name: String = ""
    @State private var adresse: String = ""
    @State var telefonnummern: [String] = [] //<-
    @State var something: String = ""
    var body: some View {
        VStack {
            List {
                TextField("Name/Vorname", text: $name)
                TextField("Adresse", text: $adresse)
                
                ForEach($telefonnummern, id: \.self) {$telefonnummer in
                    TextField("Telefonnummer", text: $telefonnummer)
                }
                Button(action: {
                    telefonnummern.append("")
                }) {
                    Image(systemName: "plus.circle.fill")
                        .foregroundColor(Color(.systemGreen))
                }
            }
        } //End VStack
    }//End body
}//End `HeartTextSquareView`

When you want variable number of UI elements, you need to prepare a variable each element of which represents each UI element.

  • Thank you much times I will try it out. I am not so experienced with Swift, I am 16 and I am a beginner with Swift.

  • Many people asking here are not so experienced with Swift and are beginners, so you have no need to mind that. Learning programming is a long way to go, maybe years. But you can learn how to use this site far more quickly. Have you ever visited the support page of the dev forums? Or reading some articles of StackOverflow will also help. How do I ask a good question?.

  • Yes, I know StackOverflow but I will look at the other posts. Thanks for the tips

Add a Comment

Then these problems will occur. And I don't understand why. Can someone help me?