0Pricing
SwiftUI Academy · Lección

Vinculación de un TextField al estado

Conecte un campo de texto a una cadena @State.

Vinculación de un TextField al estado es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de SwiftUI Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de SwiftUI Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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. 🎉

Preguntas frecuentes

¿La lección «Vinculación de un TextField al estado» es gratis?

Sí — el texto completo de «Vinculación de un TextField al estado» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de SwiftUI Academy, actualiza a CoddyKit PRO. El curso de SwiftUI Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Vinculación de un TextField al estado»?

Conecte un campo de texto a una cadena @State. Practicas SwiftUI Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar SwiftUI Academy?

No se requiere experiencia previa. SwiftUI Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «Vinculación de un TextField al estado»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de SwiftUI Academy?

Sí. Cada lección de SwiftUI Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Vinculación de un TextField al estado
  2. Tipos de teclado y marcadores de posición
  3. SecureField para contraseñas
  4. Validación de entrada en directo
← Volver a SwiftUI Academy