0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ナビゲーションリンク使ったら値渡せる

Last updated at Posted at 2023-09-18

値を渡すといえば、SwiftUIでは基本となるのが@Binding(varやletでも渡すことが可能)

struct ParentView: View {
    @State private var value = 42

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

struct ChildView: View {
    @Binding var value: Int

    var body: some View {
        Text("Value: \(value)")
    }
}

ナビゲーションリンクで値を渡す際はこのように書く

import SwiftUI

struct ContentView: View {
    @State private var valueToPass = "Hello, World!"

    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailView(passedValue: valueToPass)) {
                Text("Go to Detail View")
            }
        }
    }
}

struct DetailView: View {
    var passedValue: String

    var body: some View {
        Text("Passed Value: \(passedValue)")
    }
}

でも最近ナビゲーションリンクじゃなくてナビゲーションマネージャー使うようになったからこれもつかわなくなってきた。

以上。

9/21 記事訂正済み

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?