SwiftUI Academy · บทเรียน

การส่งข้อมูลไปยัง View รายละเอียด

ส่งรายการที่เลือกไปยังหน้าจอถัดไป

บทเรียน 2 จาก 413 ขั้นตอน

การส่งข้อมูลไปยัง View รายละเอียด เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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. 🎯

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “การส่งข้อมูลไปยัง View รายละเอียด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การส่งข้อมูลไปยัง View รายละเอียด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การส่งข้อมูลไปยัง View รายละเอียด”

ส่งรายการที่เลือกไปยังหน้าจอถัดไป คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การส่งข้อมูลไปยัง View รายละเอียด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเปิดหน้าจอด้วย NavigationLink
  2. การส่งข้อมูลไปยัง View รายละเอียด
  3. แถบเครื่องมือและชื่อการนำทาง
  4. เส้นทางการนำทางโดยใช้โค้ด
← กลับไปที่ SwiftUI Academy