กล่องโต้ตอบยืนยัน
เสนอตัวเลือกที่มีผลทำลายล้างด้วย confirmationDialog
กล่องโต้ตอบยืนยัน เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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! 🎉
คำถามที่พบบ่อย
บทเรียน “กล่องโต้ตอบยืนยัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “กล่องโต้ตอบยืนยัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “กล่องโต้ตอบยืนยัน”
เสนอตัวเลือกที่มีผลทำลายล้างด้วย confirmationDialog คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “กล่องโต้ตอบยืนยัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การแสดงชีต
- จุดหยุดและตัวบ่งชี้การลาก
- การแจ้งเตือนพร้อมการดำเนินการ
- กล่องโต้ตอบยืนยัน