0Pricing
SwiftUI Academy · บทเรียน

ส่งการเชื่อมโยงข้อมูลลงไป

ส่งสถานะไปยังมุมมองย่อยด้วยเครื่องหมายดอลลาร์

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

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

The Dollar Sign

To turn @State into a binding you pass it, prefix the property with a dollar sign. That hands the child a live connection.

Value vs Binding

Plain name gives the current value. The dollar-prefixed name gives the binding. Same property, two ways to use it.

var count = 0
// count   -> the value
// $count  -> the binding

Owning the State

The parent declares the source with @State. Only the owner of state can create a binding from it.

struct Parent: View {
    @State private var isOn = false
    var body: some View { Text("Placeholder") }
}

Handing It Off

Inside the parents body, pass the binding into the child by writing the property name with a dollar sign in front.

var body: some View {
    ChildView(isOn: $isOn)
}

The Child Receives It

The child stores the connection in a @Binding property. Its type matches the value, like Bool or String.

struct ChildView: View {
    @Binding var isOn: Bool
    var body: some View { Toggle("On", isOn: $isOn) }
}

Types Must Match

A binding to a Bool plugs only into a @Binding var isOn: Bool. Mismatched types stop the build until you fix them.

Editing Flows Up

When the child flips its toggle, the change travels straight back to the parents @State. The parent re-renders automatically.

Forgot the Dollar Sign?

Pass the bare value where a binding is expected and Swift complains. The dollar sign is what makes it a binding.

Bindings Go Deep

A child can pass its binding further down to its own children. The same dollar sign threads state through many layers.

Read in the Child

Inside the child, use the plain name to read the current value and the dollar name when a control needs the binding back.

Text(isOn ? "On" : "Off")
Toggle("Power", isOn: $isOn)

One Line, Two Way

That single dollar-prefixed argument wires up full two-way sync. Tiny syntax, big power behind the binding. ⚡

Quick Check

Quick check on passing bindings.

Recap: The Dollar Sign Bridge

Own state with @State, pass it down with the dollar sign, receive it as @Binding. That bridge keeps parent and child in sync. 🌉

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

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

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

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

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

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

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

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

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

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

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

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

  1. เหตุใดมุมมองลูกจึงต้องใช้ @Binding
  2. ส่งการเชื่อมโยงข้อมูลลงไป
  3. สร้างแถวสวิตช์ที่นำกลับมาใช้ใหม่ได้
  4. @State กับ @Binding
← กลับไปที่ SwiftUI Academy