Meneruskan Data ke View Detail
Kirim item yang dipilih ke layar berikutnya.
Meneruskan Data ke View Detail adalah pelajaran SwiftUI Academy gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar SwiftUI Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus SwiftUI Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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. 🎯
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Meneruskan Data ke View Detail” gratis?
Ya — teks lengkap “Meneruskan Data ke View Detail” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus SwiftUI Academy, upgrade ke CoddyKit PRO. Kursus SwiftUI Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Meneruskan Data ke View Detail”?
Kirim item yang dipilih ke layar berikutnya. Kamu berlatih SwiftUI Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai SwiftUI Academy?
Tidak diperlukan pengalaman sebelumnya. SwiftUI Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.
Berapa lama pelajaran “Meneruskan Data ke View Detail” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran SwiftUI Academy ini?
Ya. Setiap pelajaran SwiftUI Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Membuka Layar dengan NavigationLink
- Meneruskan Data ke View Detail
- Bilah Alat dan Judul Navigasi
- Jalur Navigasi Terprogram