在使用具有 @Binding 的列表时在视图之间传递引用

huangapple go评论76阅读模式
英文:

<SwiftUI> Passing references between views when using lists with @Binding

问题

我是SwiftUI编程的新手,有一个基本问题。想知道为什么在这个例子中的“selectedIndex”值没有在子视图“TestChildView”中反映出来。我该如何修改以便在子视图中获得正确的“selectedIndex”值?

import SwiftUI

struct TestView: View {
    @State var selectedIndex: Int = 0
    var body: some View {
        NavigationStack {
            VStack {
                List {
                    Section(header:Text("Test")) {
                        ForEach(0 ..< 5, id: \.self) {selectedIndex in
                            NavigationLink {
                                TestChildView(selectedIndex: $selectedIndex)
                            } label: {
                                Text(String(selectedIndex)
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}
struct TestChildView: View {
    @Binding var selectedIndex: Int
    var body: some View {
        VStack {
            Text(String(selectedIndex))
        }
        
    }
}
英文:

I'm a newbie in SwiftUI programming and having a basic question.
Wondering why the value in "selectedIndex" in this example is not reflected in the child view, "TestChildView". How can I modify this so that I can get the correct selectedIndex value in the child view?

import SwiftUI

struct TestView: View {
    @State var selectedIndex: Int = 0
    var body: some View {
        NavigationStack {
            VStack {
                List {
                    Section(header:Text(&quot;Test&quot;)) {
                        ForEach(0 ..&lt; 5, id: \.self) {selectedIndex in
                            NavigationLink {
                                TestChildView(selectedIndex: $selectedIndex)
                            } label: {
                                Text(String(selectedIndex)
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}
struct TestChildView: View {
    @Binding var selectedIndex: Int
    var body: some View {
        VStack {
            Text(String(selectedIndex))
        }
        
    }
}

答案1

得分: 1

Currently the @State var selectedIndex ... and the selectedIndex in the ForEach loop are two different things. That is why it is not reflected in the child view, TestChildView.

To ...get the correct selectedIndex value in the child view, remove the

@State var selectedIndex: Int = 0 

and the $ in the call of TestChildView and use

TestChildView(selectedIndex: selectedIndex)

Also use let selectedIndex: Int in TestChildView

EDIT-1:

here is the full test code I use to show it works well for me.

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct TestView: View {
    
    var body: some View {
        NavigationStack {
            VStack {
                List {
                    Section(header:Text("Test")) {
                        ForEach(0..<5, id: \.self) { selectedIndex in
                            NavigationLink {
                                TestChildView(selectedIndex: selectedIndex)
                            } label: {
                                Text(String(selectedIndex))
                            }
                        }
                    }
                }
            }
        }
    }
}

struct TestChildView: View {
    let selectedIndex: Int
    
    var body: some View {
        VStack {
            Text(String(selectedIndex))
        }
    }
}


struct ContentView: View {
    var body: some View {
        TestView()
    }
}
英文:

Currently the @State var selectedIndex ... and the selectedIndex in the ForEach loop are two different things. That is why it is not reflected in the child view, TestChildView.

To ...get the correct selectedIndex value in the child view, remove the

@State var selectedIndex: Int = 0 

and the $ in the call of TestChildView and use

TestChildView(selectedIndex: selectedIndex)

Also use let selectedIndex: Int in TestChildView

EDIT-1:

here is the full test code I use to show it works well for me.

 import SwiftUI

 @main
 struct TestApp: App {
     var body: some Scene {
         WindowGroup {
             ContentView()
         }
     }
 }

struct TestView: View {
    
    var body: some View {
        NavigationStack {
            VStack {
                List {
                    Section(header:Text(&quot;Test&quot;)) {
                        ForEach(0..&lt;5, id: \.self) { selectedIndex in
                            NavigationLink {
                                TestChildView(selectedIndex: selectedIndex)
                            } label: {
                                Text(String(selectedIndex))
                            }
                        }
                    }
                }
            }
        }
    }
}

struct TestChildView: View {
    let selectedIndex: Int
    
    var body: some View {
        VStack {
            Text(String(selectedIndex))
        }
    }
}


struct ContentView: View {
    var body: some View {
        TestView()
    }
}

huangapple
  • 本文由 发表于 2023年7月3日 15:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定