SwiftUI forms: TextField

The first form control we’ll see is the simplest: TextField.

This lets us show some text, like the Text view, and it can be editable by the user, so we can get input in the form of text.

Here’s the most basic example of TextField…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

The first form control we’ll see is the simplest: TextField.

This lets us show some text, like the Text view, and it can be editable by the user, so we can get input in the form of text.

Here’s the most basic example of TextField:

struct ContentView: View {
    @State private var name = ""
    
    var body: some View {
        Form {
            TextField("", text: $name)
        }
    }
}

We have a SwiftUI: properties that we prefaced with the @State property wrapper.

Run the code. You can see an empty text field. You can tap on it:

And you can enter any text inside it:

The first argument of TextField is a string that’s visualized when the field is empty. You can fill it with any text you want, like this:

TextField("Your name", text: $name)


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-09-25T05:00:00+00:00) SwiftUI forms: TextField. Retrieved from https://www.scien.cx/2021/09/25/swiftui-forms-textfield/

MLA
" » SwiftUI forms: TextField." flaviocopes.com | Sciencx - Saturday September 25, 2021, https://www.scien.cx/2021/09/25/swiftui-forms-textfield/
HARVARD
flaviocopes.com | Sciencx Saturday September 25, 2021 » SwiftUI forms: TextField., viewed ,<https://www.scien.cx/2021/09/25/swiftui-forms-textfield/>
VANCOUVER
flaviocopes.com | Sciencx - » SwiftUI forms: TextField. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/25/swiftui-forms-textfield/
CHICAGO
" » SwiftUI forms: TextField." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/09/25/swiftui-forms-textfield/
IEEE
" » SwiftUI forms: TextField." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/09/25/swiftui-forms-textfield/. [Accessed: ]
rf:citation
» SwiftUI forms: TextField | flaviocopes.com | Sciencx | https://www.scien.cx/2021/09/25/swiftui-forms-textfield/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.