向下传递 Binding
使用美元符号将状态传入子视图。
向下传递 Binding 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 bindingOwning 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 导师学习 Swift — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 30
- 课程
- 120
常见问题解答
「向下传递 Binding」课时是免费的吗?
是的 — 「向下传递 Binding」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。
「向下传递 Binding」这节课中我会学到什么?
使用美元符号将状态传入子视图。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 SwiftUI Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「向下传递 Binding」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 SwiftUI Academy 课中编写并运行代码吗?
能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 子视图为何需要 @Binding
- 向下传递 Binding
- 构建可复用的切换行
- @State 与 @Binding