0Pricing
SwiftUI Academy · บทเรียน

การแจ้งเตือนพร้อมการดำเนินการ

แสดงการแจ้งเตือนและตอบสนองเมื่อแตะปุ่ม

การแจ้งเตือนพร้อมการดำเนินการ เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What Is an Alert?

An alert is a small pop-up that grabs attention with a title, a message, and one or more buttons the user must respond to. ⚠️

Alerts Use a Boolean

Like sheets, an alert shows when a boolean state turns true. Store that flag in your view and flip it to trigger the pop-up.

@State private var showAlert = false

The .alert Modifier

Attach .alert to a view, give it a title, and bind it to your boolean. SwiftUI presents the alert when the flag is true.

.alert("Saved!", isPresented: $showAlert) {
    Button("OK") { }
}

Adding a Button

Every alert needs at least one button. A plain OK button dismisses the alert and runs whatever code you put in its action.

Button("OK") { print("User confirmed") }

A Cancel Button

Mark a button with role .cancel to give it a clear way out. It appears bold and lets users back away safely.

Button("Cancel", role: .cancel) { }

A Destructive Button

The .destructive role turns a button red, signaling a risky action like deleting. Use it so users notice before they tap.

Button("Delete", role: .destructive) { remove() }

Adding a Message

Pass a second closure to .alert to show explanatory text under the title. Keep it short and clear so users decide quickly.

.alert("Delete file?", isPresented: $show) {
    Button("Delete", role: .destructive) { }
} message: {
    Text("This cannot be undone.")
}

Multiple Buttons

An alert can hold several buttons stacked together. SwiftUI arranges them and runs the matching action when one is tapped.

Button("Delete", role: .destructive) { }
Button("Cancel", role: .cancel) { }

Reacting to a Tap

Each button's action closure runs when tapped, then the alert closes automatically. Put your save, delete, or retry logic there.

Button("Retry") { reloadData() }

Alerts From an Error

Drive an alert from an optional error with .alert(_:isPresented:presenting:). The alert appears whenever the error value is non-nil.

Keep Alerts Rare

Alerts interrupt the user, so save them for important moments like errors or confirmations. Overusing them feels noisy and annoying.

Quick Check

Which button role turns an alert button red?

Recap: Alerts

You learned to show an alert from a boolean, add titles and messages, and use cancel and destructive button roles. Well done! 🎉

คำถามที่พบบ่อย

บทเรียน “การแจ้งเตือนพร้อมการดำเนินการ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแจ้งเตือนพร้อมการดำเนินการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแจ้งเตือนพร้อมการดำเนินการ”

แสดงการแจ้งเตือนและตอบสนองเมื่อแตะปุ่ม คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การแจ้งเตือนพร้อมการดำเนินการ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การแสดงชีต
  2. จุดหยุดและตัวบ่งชี้การลาก
  3. การแจ้งเตือนพร้อมการดำเนินการ
  4. กล่องโต้ตอบยืนยัน
← กลับไปที่ SwiftUI Academy