Ein TextField an State binden
Verbinden Sie ein Textfeld mit einem @State-String.
Ein TextField an State binden ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Meet TextField
A TextField is SwiftUI's box where users type. Give it a prompt and somewhere to store what they enter, and you have live text input. ✍️
Text Needs a Home
Typed characters must go somewhere. You store them in a @State string that the text field reads from and writes to as the user types.
@State private var name = ""The Binding Connection
A TextField needs a two-way link called a binding. It both reads the current value and writes new keystrokes back into your state.
The Dollar Sign
To pass a binding, prefix the property with $. Writing $name hands the text field a two-way connection instead of a plain copy.
TextField("Name", text: $name)The Placeholder
The first argument is the placeholder: faint hint text shown when the field is empty. It disappears the moment the user starts typing.
TextField("Enter your name", text: $name)Reading the Value
Because the field writes into your state, you can read name anywhere in the body to react to what the user has typed so far.
Text("Hello, \(name)")A Bordered Look
Add the textFieldStyle modifier with .roundedBorder to give the field a clean, tappable outline instead of a bare line.
TextField("Name", text: $name)
.textFieldStyle(.roundedBorder)Live Updates Are Free
You never write code to refresh the screen. When the binding changes the state, SwiftUI re-renders the view automatically for you. ✨
One Source of Truth
The @State string is the single source of truth. The field and any Text that reads it always stay perfectly in sync.
Plain Value vs Binding
Writing name passes a read-only copy. Writing $name passes the binding the field needs to send edits back. The dollar sign matters.
Putting It Together
A state string, a TextField bound with $, and a Text that reads it: that is a complete, reactive input screen.
TextField("Name", text: $name)
Text("Hi, \(name)")Quick Check
How do you give a TextField a two-way link to a @State string?
Recap
You bound a TextField to @State with the $ prefix, so typing updates your value and the UI refreshes itself. That is reactive input in a nutshell. 🎉
Häufig gestellte Fragen
Ist die Lektion „Ein TextField an State binden“ kostenlos?
Ja — der vollständige Text von „Ein TextField an State binden“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Ein TextField an State binden“?
Verbinden Sie ein Textfeld mit einem @State-String. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um SwiftUI Academy zu starten?
Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Ein TextField an State binden“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?
Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Ein TextField an State binden
- Tastaturtypen und Platzhalter
- SecureField für Passwörter
- Live-Eingabevalidierung