Skip to content
Prepare to Grow

SwiftUI Fundamentals

Sheets, Alerts, Toolbars, and Presentations

Present temporary workflows and system messages with data-driven sheets, alerts, confirmation dialogs, and semantic toolbar actions.

Intermediate6 min read2 guided activities

Before you begin

What you will understand

  • Choose between pushed and modal presentation.
  • Present model-driven sheets with item.
  • Use alerts and confirmation dialogs for appropriate decisions.
  • Place actions semantically with toolbar roles.
01

Learning stage

Concept Explanation

A pushed destination extends the current hierarchy; a sheet presents a temporary or self-contained task. Use fullScreenCover only when immersive focus is justified. Presentation should follow the product relationship, not merely a desired animation.

Boolean presentation works for a simple local condition, but item-driven presentation connects visibility and content to one optional Identifiable value. When the value becomes non-nil, SwiftUI presents it; when presentation ends, the value returns to nil. This avoids separate isPresented and selectedItem state becoming inconsistent.

Alerts communicate important information or request a small decision. ConfirmationDialog is appropriate for a set of related choices, especially destructive ones. Toolbars place navigation, confirmation, cancellation, and contextual actions according to platform conventions. Button roles communicate semantics to styling and assistive technology.

  • sheet: temporary task that preserves underlying context.
  • fullScreenCover: immersive modal presentation.
  • alert: brief important message or small decision.
  • confirmationDialog: a group of choices requiring confirmation.
  • toolbar: semantic placement of screen actions.
02

Learning stage

Syntax & Examples

The selected lesson is both the presentation trigger and the data supplied to the editor, eliminating invalid Boolean-plus-selection combinations.

LessonPresentation.swiftswift
import SwiftUI

struct Lesson: Identifiable {
  let id: UUID
  var title: String
}

struct LessonLibrary: View {
  @State private var selectedLesson: Lesson?
  @State private var lessonToDelete: Lesson?

  var body: some View {
    List(sampleLessons) { lesson in
      Button(lesson.title) { selectedLesson = lesson }
        .swipeActions {
          Button("Delete", role: .destructive) { lessonToDelete = lesson }
        }
    }
    .navigationTitle("Library")
    .sheet(item: $selectedLesson) { lesson in
      NavigationStack { LessonEditor(lesson: lesson) }
    }
    .confirmationDialog(
      "Delete this lesson?",
      isPresented: Binding(
        get: { lessonToDelete != nil },
        set: { if !$0 { lessonToDelete = nil } }
      ),
      presenting: lessonToDelete
    ) { lesson in
      Button("Delete (lesson.title)", role: .destructive) { delete(lesson) }
      Button("Cancel", role: .cancel) { }
    } message: { lesson in
      Text("This action cannot be undone for (lesson.title).")
    }
  }
}
The sheet is item driven, while the destructive choice uses a confirmation dialog with explicit destructive and cancel roles.
03

Learning stage

Real-World Usage

Connect the SwiftUI concept to complete, maintainable application features.

  • Present editors, filters, sharing, account tasks, and creation flows.
  • Confirm destructive or irreversible actions.
  • Add save, cancel, add, edit, and overflow actions to toolbars.
  • Dismiss a modal from inside using the dismiss environment action after successful completion.
04

Learning stage

Common Mistakes

Identify patterns that weaken ownership, identity, adaptation, accessibility, or user experience.

  • Using both isSheetPresented and selectedItem for one presentation.
  • Presenting a sheet when the destination belongs in the navigation hierarchy.
  • Showing an alert for routine validation that should appear beside a field.
  • Using a destructive visual color without a destructive Button role.
  • Placing every action in the navigation bar without prioritization.
05

Learning stage

Practice Exercises

Build the behavior in a small Xcode project and review it in multiple states.

Activity 1

Build an item-driven editor

Present an editor sheet from a list using an optional Identifiable selection. Include Cancel and Save toolbar actions and dismiss only after valid save.

Hint: Let the selected optional value control both presentation and content.

Activity 2

Confirm a destructive action

Add a confirmation dialog for deleting a course with a clear consequence, destructive role, and cancel action.

Hint: Name the object so the user can verify what will be deleted.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

What advantage does sheet(item:) provide?

  1. A.One optional value controls both presentation and content
  2. B.It always covers the full screen
  3. C.It removes the need for identity
  4. D.It creates navigation history
Reveal answer

One optional value controls both presentation and content

The impossible state of a visible sheet with no selected item is eliminated.

QUESTION 2

Should an alert be used for ordinary inline form validation?

Reveal answer

Usually no. Validation is generally clearer near the relevant field.

Alerts interrupt the user and are better reserved for important messages or small decisions.

07

Learning stage

Interview Preparation

Explain the design in terms of data flow, identity, lifecycle, platform behavior, and trade-offs.

How do you decide between NavigationStack and a sheet?

Short answer: Use navigation for hierarchy and a sheet for a temporary, self-contained task that preserves the current context.

Explore the detailed answer

The product relationship determines the presentation. A detail usually belongs in navigation. Editing, creation, filters, or account tasks often fit a modal. I also consider whether the task needs its own internal NavigationStack and how cancellation or completion returns to the underlying context.

Possible follow-ups

  • • When is fullScreenCover appropriate?
  • • Why is item-driven presentation safer?
08

Learning stage

Key Takeaways

Presentation is part of information architecture. Model modal content from data, use system roles and toolbar placements, and reserve alerts and confirmation dialogs for decisions that truly need interruption.

  • Choose hierarchy or modality from the task relationship.
  • Prefer item-driven presentation for selected models.
  • Use alerts and dialogs for focused decisions.
  • Button roles communicate destructive and cancel semantics.
  • Toolbar placement should follow platform conventions.