Dialog Konfirmasi
Tawarkan pilihan destruktif dengan confirmationDialog.
Dialog Konfirmasi adalah pelajaran SwiftUI Academy gratis di CoddyKit. Ini adalah pelajaran 4 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.
What Is a Confirmation Dialog?
A confirmation dialog is an action sheet that slides up with a list of choices, perfect for offering several options at once. 🗂️
Dialog vs Alert
An alert centers on screen for one key decision. A dialog rises from the bottom and suits a menu of related actions instead.
Triggered by a Boolean
Like sheets and alerts, a dialog opens when a boolean state turns true. Store the flag and flip it from a button tap.
@State private var showDialog = falseThe confirmationDialog Modifier
Attach confirmationDialog to a view with a title and a boolean binding. SwiftUI presents the choices when the flag is true.
.confirmationDialog("Choose", isPresented: $showDialog) {
Button("Photo Library") { }
}Listing Choices as Buttons
Each option is a button inside the dialog. SwiftUI stacks them and runs the action of whichever one the user taps.
Button("Camera") { }
Button("Photo Library") { }A Destructive Option
Use the .destructive role for risky choices like delete. The button turns red so users notice before committing.
Button("Delete Account", role: .destructive) { }The Cancel Button
SwiftUI adds a Cancel button automatically. You can also supply your own with role .cancel to control its wording.
Button("Not now", role: .cancel) { }Adding a Title Message
Pass a message closure to explain the choice. It shows as a short caption above the buttons to guide the decision.
.confirmationDialog("Remove item?", isPresented: $show) {
Button("Remove", role: .destructive) { }
} message: {
Text("You can add it again later.")
}Reacting to a Choice
Each button's action runs on tap, then the dialog closes. Put the matching logic, like pick or delete, inside that closure.
Button("Save") { saveDocument() }Dialogs Adapt by Device
On iPhone a dialog rises as an action sheet. On iPad and Mac it appears as a popover near the control that opened it.
When to Use a Dialog
Reach for a dialog when a single tap could mean several things, like sharing or choosing an image source. It keeps options tidy.
Quick Check
When is a confirmation dialog the better choice over an alert?
Recap: Dialogs
You can now present a confirmation dialog from a boolean, list action buttons, and mark destructive choices in red. You finished the course! 🎉
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Dialog Konfirmasi” gratis?
Ya — teks lengkap “Dialog Konfirmasi” 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 “Dialog Konfirmasi”?
Tawarkan pilihan destruktif dengan confirmationDialog. 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 4 dari 4.
Berapa lama pelajaran “Dialog Konfirmasi” 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
- Menampilkan Sheet
- Detent & Indikator Geser
- Alert dengan Tindakan
- Dialog Konfirmasi