0Pricing
SwiftUI Academy · Lekcja

Alerty z akcjami

Wyświetlaj alerty i reaguj na naciśnięcia przycisków

Alerty z akcjami to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Alerty z akcjami” jest bezpłatna?

Tak — pełny tekst „Alerty z akcjami” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Alerty z akcjami”?

Wyświetlaj alerty i reaguj na naciśnięcia przycisków Ćwiczysz SwiftUI Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć SwiftUI Academy?

Nie wymagamy żadnego doświadczenia. SwiftUI Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Alerty z akcjami”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji SwiftUI Academy?

Tak. Każda lekcja SwiftUI Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Prezentowanie arkusza
  2. Detenty i wskaźniki przeciągania
  3. Alerty z akcjami
  4. Okna dialogowe potwierdzenia
← Powrót do SwiftUI Academy