0Pricing
SwiftUI Academy · Lezione

Alert con azioni

Mostri gli alert e reagisca ai tocchi sui pulsanti.

Alert con azioni è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento SwiftUI Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso SwiftUI Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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

Domande Frequenti

La lezione «Alert con azioni» è gratuita?

Sì — il testo completo di «Alert con azioni» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso SwiftUI Academy, passa a CoddyKit PRO. Il corso SwiftUI Academy include 4 lezioni in totale.

Cosa imparerò in «Alert con azioni»?

Mostri gli alert e reagisca ai tocchi sui pulsanti. Eserciti SwiftUI Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare SwiftUI Academy?

Non è richiesta alcuna esperienza precedente. SwiftUI Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Alert con azioni»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione SwiftUI Academy?

Sì. Ogni lezione SwiftUI Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Presentare una Sheet
  2. Detents e indicatori di trascinamento
  3. Alert con azioni
  4. Finestre di dialogo di conferma
← Torna a SwiftUI Academy