0Pricing
SwiftUI Academy · Pelajaran

Aktif/Nonaktif dengan Toggle

Ikat Toggle ke pengaturan boolean.

Aktif/Nonaktif dengan Toggle adalah pelajaran SwiftUI Academy gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar SwiftUI Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus SwiftUI Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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 = false

Bind 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. 🔁

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Aktif/Nonaktif dengan Toggle” gratis?

Ya — teks lengkap “Aktif/Nonaktif dengan Toggle” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus SwiftUI Academy, upgrade ke CoddyKit PRO. Kursus SwiftUI Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Aktif/Nonaktif dengan Toggle”?

Ikat Toggle ke pengaturan boolean. Kamu berlatih SwiftUI Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai SwiftUI Academy?

Tidak diperlukan pengalaman sebelumnya. SwiftUI Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.

Berapa lama pelajaran “Aktif/Nonaktif dengan Toggle” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran SwiftUI Academy ini?

Ya. Setiap pelajaran SwiftUI Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Aktif/Nonaktif dengan Toggle
  2. Memilih Nilai dengan Slider
  3. Menambah Nilai dengan Stepper
  4. Memilih Opsi dengan Picker
← Kembali ke SwiftUI Academy