0Pricing
SwiftUI Academy · レッスン

TextField を State にバインドする

テキスト フィールドを @State の文字列に接続します。

「TextField を State にバインドする」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Meet TextField

A TextField is SwiftUI's box where users type. Give it a prompt and somewhere to store what they enter, and you have live text input. ✍️

Text Needs a Home

Typed characters must go somewhere. You store them in a @State string that the text field reads from and writes to as the user types.

@State private var name = ""

The Binding Connection

A TextField needs a two-way link called a binding. It both reads the current value and writes new keystrokes back into your state.

The Dollar Sign

To pass a binding, prefix the property with $. Writing $name hands the text field a two-way connection instead of a plain copy.

TextField("Name", text: $name)

The Placeholder

The first argument is the placeholder: faint hint text shown when the field is empty. It disappears the moment the user starts typing.

TextField("Enter your name", text: $name)

Reading the Value

Because the field writes into your state, you can read name anywhere in the body to react to what the user has typed so far.

Text("Hello, \(name)")

A Bordered Look

Add the textFieldStyle modifier with .roundedBorder to give the field a clean, tappable outline instead of a bare line.

TextField("Name", text: $name)
  .textFieldStyle(.roundedBorder)

Live Updates Are Free

You never write code to refresh the screen. When the binding changes the state, SwiftUI re-renders the view automatically for you. ✨

One Source of Truth

The @State string is the single source of truth. The field and any Text that reads it always stay perfectly in sync.

Plain Value vs Binding

Writing name passes a read-only copy. Writing $name passes the binding the field needs to send edits back. The dollar sign matters.

Putting It Together

A state string, a TextField bound with $, and a Text that reads it: that is a complete, reactive input screen.

TextField("Name", text: $name)
Text("Hi, \(name)")

Quick Check

How do you give a TextField a two-way link to a @State string?

Recap

You bound a TextField to @State with the $ prefix, so typing updates your value and the UI refreshes itself. That is reactive input in a nutshell. 🎉

よくある質問

「TextField を State にバインドする」レッスンは無料ですか?

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

「TextField を State にバインドする」で何を学びますか?

テキスト フィールドを @State の文字列に接続します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「TextField を State にバインドする」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. TextField を State にバインドする
  2. キーボードの種類とプレースホルダー
  3. パスワード用の SecureField
  4. 入力内容のリアルタイム検証
← SwiftUI Academyに戻る