Vinculando um TextField ao estado
Conecte um campo de texto a uma cadeia de caracteres @State.
Vinculando um TextField ao estado é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de SwiftUI Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de SwiftUI Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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. 🎉
Perguntas Frequentes
A aula “Vinculando um TextField ao estado” é grátis?
Sim — o texto completo de “Vinculando um TextField ao estado” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de SwiftUI Academy, atualize para CoddyKit PRO. O curso de SwiftUI Academy inclui 4 aulas no total.
O que vou aprender em “Vinculando um TextField ao estado”?
Conecte um campo de texto a uma cadeia de caracteres @State. Você pratica SwiftUI Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar SwiftUI Academy?
Nenhuma experiência prévia é necessária. SwiftUI Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.
Quanto tempo leva a aula “Vinculando um TextField ao estado”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de SwiftUI Academy?
Sim. Cada aula de SwiftUI Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Vinculando um TextField ao estado
- Tipos de teclado e textos de indicação
- SecureField para senhas
- Validação de entrada em tempo real