Criando uma Linha de Alternância Reutilizável
Crie uma visualização filha que altere o estado da visualização pai.
Criando uma Linha de Alternância Reutilizável é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 3 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.
A Component That Edits
Lets build a reusable row that flips a setting. The trick: the row mutates the parents state through a @Binding. 🧩
Design the Inputs
The row needs a label to show and a binding to change. So it takes a title string and a Bool binding.
Declare the Child View
Create a struct with a plain title and a binding for the value it will toggle on and off.
struct ToggleRow: View {
let title: String
@Binding var isOn: Bool
var body: some View { Text("Placeholder") }
}Build the Body
Inside the body, a single Toggle shows the title and writes through the binding when tapped.
var body: some View {
Toggle(title, isOn: $isOn)
.padding()
}Use It in a Parent
The parent owns the setting with @State, then drops the row in and passes the value with a dollar sign.
struct SettingsView: View {
@State private var notify = true
var body: some View { ToggleRow(title: "Notify", isOn: $notify) }
}Reuse It Everywhere
Now add ten settings with ten rows, each bound to its own state. One small view, reused across the whole screen.
ToggleRow(title: "Sound", isOn: $sound)
ToggleRow(title: "Vibrate", isOn: $vibrate)Parent Sees Every Change
Flip any row and the parents matching @State updates instantly. The parent can react to the new value right away.
Text(notify ? "On" : "Off")
.foregroundStyle(notify ? .green : .gray)Keep the Child Dumb
The row holds no opinions about where data lives. It just edits the binding it was given, which makes it portable.
Add a Little Style
Because the row is one view, you can style it once and every instance looks the same. Consistency comes for free. 💅
Toggle(title, isOn: $isOn)
.tint(.orange)
.font(.headline)Swap the Type Later
Want a row for a number instead? Change the binding type and the same pattern works for any editable value.
Small Pieces, Big Apps
Reusable binding-driven rows are how real apps stay tidy. Compose many tiny editors into one clean settings screen.
Quick Check
Quick check on the toggle row.
Recap: A Reusable Editor
A child view with a @Binding becomes a reusable editor. Pass state in with the dollar sign and drop it in anywhere. 🎉
Perguntas Frequentes
A aula “Criando uma Linha de Alternância Reutilizável” é grátis?
Sim — o texto completo de “Criando uma Linha de Alternância Reutilizável” é 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 “Criando uma Linha de Alternância Reutilizável”?
Crie uma visualização filha que altere o estado da visualização pai. 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 3 de 4.
Quanto tempo leva a aula “Criando uma Linha de Alternância Reutilizável”?
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
- Por que as Filhas Precisam de @Binding
- Passando uma Binding para Baixo
- Criando uma Linha de Alternância Reutilizável
- @State versus @Binding