Tworzenie wielokrotnego użytku wiersza przełącznika
Utwórz widok podrzędny, który modyfikuje stan widoku nadrzędnego
Tworzenie wielokrotnego użytku wiersza przełącznika to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🎉
Często zadawane pytania
Czy lekcja „Tworzenie wielokrotnego użytku wiersza przełącznika” jest bezpłatna?
Tak — pełny tekst „Tworzenie wielokrotnego użytku wiersza przełącznika” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Tworzenie wielokrotnego użytku wiersza przełącznika”?
Utwórz widok podrzędny, który modyfikuje stan widoku nadrzędnego Ćwiczysz SwiftUI Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć SwiftUI Academy?
Nie wymagamy żadnego doświadczenia. SwiftUI Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Tworzenie wielokrotnego użytku wiersza przełącznika”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji SwiftUI Academy?
Tak. Każda lekcja SwiftUI Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Dlaczego elementy podrzędne potrzebują @Binding
- Przekazywanie Binding w dół hierarchii
- Tworzenie wielokrotnego użytku wiersza przełącznika
- @State a @Binding