SwiftUI Academy · レッスン

バインディングを下位ビューに渡す

ドル記号を使ってサブビューに状態を渡します。

レッスン 2/413 ステップ

「バインディングを下位ビューに渡す」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 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 チューターと学ぶ Swift — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
30
レッスン
120

よくある質問

「バインディングを下位ビューに渡す」レッスンは無料ですか?

はい。「バインディングを下位ビューに渡す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「バインディングを下位ビューに渡す」で何を学びますか?

ドル記号を使ってサブビューに状態を渡します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「バインディングを下位ビューに渡す」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 子ビューに@Bindingが必要な理由
  2. バインディングを下位ビューに渡す
  3. 再利用可能なトグル行を作る
  4. @Stateと@Binding
← SwiftUI Academyに戻る