Menu
Coddy logo textTech

Toggle

Part of the Introduction to SwiftUI section of Coddy's Swift journey. Lesson 25 of 50.

A Toggle is an on/off switch bound to a Bool:

@State private var notifications = true

Toggle("Notifications", isOn: $notifications)

The title is shown on the left and the switch on the right. Flipping the switch writes to notifications through the binding, and anything that reads the value updates:

Text(notifications ? "You will get alerts" : "Alerts are off")
challenge icon

Challenge

Easy

Add a Toggle titled Wi-Fi, bound to wifi, with the identifier wifi. The status text must read Connected while the toggle is on and Offline while it is off.

Try it yourself

import SwiftUI

@main
struct CoddyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction to SwiftUI

Practice on your own: Swift playground