Attivare e disattivare con Toggle
Collegare un Toggle a un'impostazione booleana.
Attivare e disattivare con Toggle è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento SwiftUI Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso SwiftUI Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Meet the Toggle
A Toggle is SwiftUI's on/off switch. It is perfect for settings like notifications, dark mode, or any yes-or-no choice. 🔘
Toggle Needs a Boolean
Every Toggle reflects a Bool value. When the switch is on the value is true; when it is off the value is false. They stay in sync.
Store It in @State
Hold the on/off value in a @State property so the view can read and update it as the user flips the switch.
@State private var isOn = falseBind with the Dollar Sign
Connect the Toggle to your state using the dollar sign. That creates a two-way binding so flips update the value automatically.
Toggle("Notifications", isOn: $isOn)Give It a Clear Label
The first argument is the label users read next to the switch. Keep it short and describe exactly what the toggle controls.
Toggle("Dark Mode", isOn: $isDark)React to the Value
Read the boolean anywhere in your body to react to the switch. Here the text changes based on whether the toggle is on.
Text(isOn ? "On" : "Off")Put It in a Form
Toggles look most at home inside a Form, where they automatically gain the familiar iOS settings-row styling.
Form {
Toggle("Wi-Fi", isOn: $wifiOn)
}A Richer Label View
Need an icon too? Use the closure form so the label can be any view, like a Label with an SF Symbol.
Toggle(isOn: $isOn) {
Label("Sound", systemImage: "speaker")
}Style the Switch
Change the look with toggleStyle. The button style turns it into a tappable button instead of the default switch.
Toggle("Star", isOn: $isOn)
.toggleStyle(.button)Tint the Track
Set the on-color of the switch with the tint modifier, so it matches your app's accent and feels branded.
Toggle("On", isOn: $isOn)
.tint(.green)The Full View
Here it all comes together: one boolean, one Toggle bound to it, and a Text that responds to every flip.
struct SettingsView: View {
@State private var isOn = false
var body: some View {
Toggle("Alerts", isOn: $isOn)
Text(isOn ? "On" : "Off")
}
}Quick Check
How do you connect a Toggle to a boolean state value?
Recap
A Toggle binds to a Bool with the dollar sign, flips it on tap, and lets you react, style, and tint the switch. 🔁
Domande Frequenti
La lezione «Attivare e disattivare con Toggle» è gratuita?
Sì — il testo completo di «Attivare e disattivare con Toggle» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso SwiftUI Academy, passa a CoddyKit PRO. Il corso SwiftUI Academy include 4 lezioni in totale.
Cosa imparerò in «Attivare e disattivare con Toggle»?
Collegare un Toggle a un'impostazione booleana. Eserciti SwiftUI Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare SwiftUI Academy?
Non è richiesta alcuna esperienza precedente. SwiftUI Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Attivare e disattivare con Toggle»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione SwiftUI Academy?
Sì. Ogni lezione SwiftUI Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Attivare e disattivare con Toggle
- Scegliere valori con Slider
- Incrementare con Stepper
- Selezionare opzioni con Picker