0Pricing
SwiftUI Academy · Lesson

Passing Data to Detail Views

Send the selected item to the next screen.

Passing Data to Detail Views is a free SwiftUI Academy lesson on CoddyKit — lesson 2 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.

Detail Screens Need Data

A detail screen is only useful if it knows which item to show. So when you push it, you hand the selected piece of data straight into the new view. 📦

Pass Through the Initializer

The cleanest way to send data is the detail view initializer. Store the value in a property, then read it inside the body.

struct DetailView: View {
    let title: String
    var body: some View { Text(title) }
}

Wire It in the Link

In the NavigationLink destination, create the detail view and pass the value in. The chosen item flows directly into the next screen.

NavigationLink("Open") {
    DetailView(title: "Welcome")
}

Passing a Whole Model

You are not limited to strings. Pass an entire struct so the detail screen has every field it needs at once.

struct Book { let title: String; let author: String }
DetailView(book: selectedBook)

Looping With Data

Inside a ForEach you can hand each item to its own link, so every row pushes a detail view filled with that exact element.

ForEach(books) { book in
    NavigationLink(book.title) { DetailView(book: book) }
}

Value Types Copy Safely

Because structs are value types, the detail view gets its own copy. Reading it cannot accidentally change the list behind you.

Show Real Detail

With the model in hand, the detail view can display anything: title, author, description, even computed text built from those fields.

var body: some View {
    VStack {
        Text(book.title).font(.title)
        Text(book.author)
    }
}

Prefer the Newer Pattern

For data-driven links, attach a value and let a navigationDestination decide the view. It keeps your row code short and reusable.

NavigationLink(book.title, value: book)

Match the Type

A navigationDestination handles one data type. SwiftUI looks at the value type you pushed and routes it to the matching destination builder.

.navigationDestination(for: Book.self) { book in
    DetailView(book: book)
}

Keep Detail Views Focused

A detail view should receive its data, not fetch it. Passing data in keeps the screen reusable and easy to preview with sample values.

Watch the Direction

Data flows forward, into the detail. To send changes back to the parent later, you will reach for bindings, which come in a future course.

Quick Check

What is the simplest way to give a detail view the item it should display?

Recap: Passing Data

You pass the chosen item into the detail view through its initializer, often a whole struct, so each screen shows exactly the right content. 🎯

Frequently asked questions

Is the “Passing Data to Detail Views” lesson free?

Yes — the full text of “Passing Data to Detail Views” 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 “Passing Data to Detail Views”?

Send the selected item to the next screen. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Passing Data to Detail Views” 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. Pushing Screens with NavigationLink
  2. Passing Data to Detail Views
  3. Toolbar & Navigation Title
  4. Programmatic Navigation Paths
← Back to SwiftUI Academy