Daten an Detailansichten übergeben
Senden Sie das ausgewählte Element an den nächsten Bildschirm.
Daten an Detailansichten übergeben ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Lerne Swift mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 30
- Lektionen
- 120
Häufig gestellte Fragen
Ist die Lektion „Daten an Detailansichten übergeben“ kostenlos?
Ja — der vollständige Text von „Daten an Detailansichten übergeben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Daten an Detailansichten übergeben“?
Senden Sie das ausgewählte Element an den nächsten Bildschirm. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um SwiftUI Academy zu starten?
Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Daten an Detailansichten übergeben“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?
Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Mit NavigationLink zu Bildschirmen wechseln
- Daten an Detailansichten übergeben
- Toolbar und Navigationstitel
- Programmgesteuerte Navigationspfade