0Pricing
SwiftUI Academy · Lesson

Alerts with Actions

Show alerts and respond to button taps.

Alerts with Actions is a free SwiftUI Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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! 🎉

Frequently asked questions

Is the “Alerts with Actions” lesson free?

Yes — the full text of “Alerts with Actions” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.

What will I learn in “Alerts with Actions”?

Show alerts and respond to button taps. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SwiftUI Academy?

No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Alerts with Actions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SwiftUI Academy lesson?

Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Presenting a Sheet
  2. Detents & Drag Indicators
  3. Alerts with Actions
  4. Confirmation Dialogs
← Back to SwiftUI Academy