詳細ビューへのデータ受け渡し
選択した項目を次の画面に渡します。
「詳細ビューへのデータ受け渡し」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. 🎯
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フィードバックを取得できます。ローカル設定は不要です。